AWT-EventQueue-0 java.lang.NullPointerException when connecting to another class -
basically i've got error:
run:exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ paintv2.uipanel$1.actionperformed(paintgui.java:line marked) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2022) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2346) @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402) @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252) @ java.awt.component.processmouseevent(component.java:6527) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3321) @ java.awt.component.processevent(component.java:6292) @ java.awt.container.processevent(container.java:2234) @ java.awt.component.dispatcheventimpl(component.java:4883) @ java.awt.container.dispatcheventimpl(container.java:2292) @ java.awt.component.dispatchevent(component.java:4705) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4898) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4533) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4462) @ java.awt.container.dispatcheventimpl(container.java:2278) @ java.awt.window.dispatcheventimpl(window.java:2739) @ java.awt.component.dispatchevent(component.java:4705) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:746) @ java.awt.eventqueue.access$400(eventqueue.java:97) @ java.awt.eventqueue$3.run(eventqueue.java:697) @ java.awt.eventqueue$3.run(eventqueue.java:691) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:86) @ java.awt.eventqueue$4.run(eventqueue.java:719) @ java.awt.eventqueue$4.run(eventqueue.java:717) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75) @ java.awt.eventqueue.dispatchevent(eventqueue.java:716) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82)'
i trying link gui colour class making paint application. questions are: has gone wrong code? , if knows how fix it, can explain how fixed please
my main method:
package paintv2; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.hashmap; public class paintgui{ public static void main(string[] args ){ jframe frame = new jframe("test panel 2"); mainpanel panel = new mainpanel(); uipanel uip = new uipanel(panel); frame.setlayout(new borderlayout()); frame.add(uip, borderlayout.north); frame.add(panel, borderlayout.center); frame.setsize(900, 900); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } }
my mainpanel class:
class mainpanel extends jpanel { int px, py; // radius public color colvals; public colour col; public mainpanel(){ this.addmousemotionlistener(new mousemotionadapter() { // store drag coordinates , repaint public void mousedragged( mouseevent event ) { px = event.getx(); py = event.gety(); repaint(); } }); // end call addmousemotionlistener } public void paint( graphics g ) { g.setcolor(color.red); g.filloval( px, py, 15, 15 ); } }
my uipanel class:
class uipanel extends jpanel { public mainpanel gpanel; public integer data; public color colval; public colour col; public uipanel(mainpanel panel) { gpanel = panel; box btnbox = box.createhorizontalbox(); btnbox.add(box.createhorizontalglue()); jbutton setbtn = new jbutton("set"); //code colour chooser //decided use dropdow list hashmap final defaultcomboboxmodel colour = new defaultcomboboxmodel(); colour.addelement("red"); colour.addelement("blue"); colour.addelement("yellow"); colour.addelement("green"); final jcombobox colours = new jcombobox(colour); jscrollpane colourscroll = new jscrollpane(colours); jbutton freebtn = new jbutton("freehand"); jbutton rectbtn = new jbutton("rectangle"); jbutton circbtn = new jbutton("circle"); jbutton linebtn = new jbutton("line"); btnbox.add(setbtn); btnbox.add(colourscroll); btnbox.add(freebtn); btnbox.add(rectbtn); btnbox.add(circbtn); btnbox.add(linebtn); btnbox.setsize(300, 100); btnbox.add(box.createhorizontalglue()); add(btnbox, borderlayout.north); setbtn.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e) { data= colours.getselectedindex(); col.setcol(data);//this error being flagged } }); } }
and colour class
package paintv2; import java.awt.*; import java.awt.event.*; import java.util.hashmap; import javax.swing.*; public class colour{ public color colval; hashmap<integer, color> map = new hashmap<integer, color>(); public colour() { map.put(0, color.red); map.put(1, color.blue); map.put(2, color.yellow); map.put(3, color.green); } public color setcol(integer data) { return map.get(data); } }
sorry in advance massive chunks of code , code being messy. if need me edit bits out make more understandable, post or , edit when able to.
seems declared col
, not initialized. try this..
public void actionperformed(actionevent e) { data= colours.getselectedindex(); if(col == null) col = new colour(); col.setcol(data);//this error being flagged }
Comments
Post a Comment