swing - Java: How to use a cardholder with multiple classes -
i'm programming in java, trying use cardholder in order switch between 2 jpanels each extension of own class. think understand basic concepts having errors in current revision, when calling classes. i'm getting null pointer exception , think it's structural problem i'm not sure how or why.
the main method points class
public class skeleton implements actionlistener{ jpanel cardholder; cardlayout cards; string carda = "a"; string cardb = "b"; jpanel jboard; jpanel jmenu; jframe frame2; board board; menu menu; boolean menuset; boolean boardset; timer timer; public class switcher implements actionlistener{ string card; switcher(string card){ this.card = card; } @override public void actionperformed(actionevent e) { cards.show(cardholder, card); } } public skeleton(jframe frame){ jpanel menu = new menu(); jpanel board = new board(); jframe frame2 = frame; timer = new timer(5, this); timer.start(); cardholder = new jpanel(); cards = new cardlayout(); cardholder.setlayout(cards); cardholder.add(menu, carda); cardholder.add(board, cardb); frame2.add(cardholder); frame2.revalidate(); frame2.setvisible(true); } public jframe getskeleton(){ return frame2; } public jpanel getcardholder(){ return cardholder; } public void checkstatus(){ if (menuset == true){ new switcher(cardb); boardset = false; } if (boardset == true){ new switcher(carda); menuset = false; } } @override public void actionperformed(actionevent e) { menuset = menu.getmenuset(); boardset = board.getboardset(); checkstatus(); } }
this board class, 1 of jpanels i'm trying switch between
public class board extends jpanel{ boolean boardset; menu menu = new menu(); public board(){ setbackground(color.white); } public jpanel getpanel(){ return this; } public void setboardset(boolean x){ boardset = x; } public boolean getboardset(){ return boardset; } }
here other jpanel class, contains button used switch other jpanel class. original starting jpanel used.
public class menu extends jpanel implements actionlistener{ boolean menuset; public menu(){ setbackground(color.black); jbutton button = new jbutton("hello"); button.addactionlistener(this); this.add(button); } public jpanel getpanel(){ return this; } @override public void actionperformed(actionevent e) { menuset = true; } public void setmenuset(boolean x){ menuset = x; } public boolean getmenuset(){ return menuset; } }
like said, i'm getting null pointer exception. occuring on line of skeleton() class
menuset = menu.getmenuset();
the line above right after actionperformed event above (from timer), , have tested timer little, works doing basic print statements whenever try use 'menu' or 'board' instance inside actionperformed, null pointer exception.
i appreciate advice. idea way i'm doing may little convoluted. if has suggestions on better way helpful. main goal able call 2 separate classes 1 main class containing cardholder. way can separate code in order keep isolated , in order.
your skeleton class has "menu" member isn't set anywhere can see. constructor declares own "menu" local variable, local constructor , hides member. setting "menu" inside constructor won't set member. don't see anywhere else "menu" member set, unless i've missed or unless class in same package setting it.
Comments
Post a Comment