java - How to use a button to call a method from another class? -
i'm pretty new java, i'm trying make basic ui. can use jbuttons call joptionpanes easily, can't use them call class.
currently have main class, ui class, , class handles data.
in class handles data have method performs basic sum on 2 elements of array:
public void firstmonth(){ string ans; int num1 = integer.parseint(sorteddata[0][0][0]); int num2 = integer.parseint(sorteddata[0][0][1]); int sum = num1 * num2; ans=string.format("the sum of day1 , day2 is: %s", sum); joptionpane.showmessagedialog(null, ans, "title", joptionpane.plain_message); }
then in ui class try , call when 1 of buttons pressed:
public class userinterface extends jframe { getdata = new getdata(); private class myhandler implements actionlistener{ public void actionperformed(actionevent event){ if(event.getsource()==button1){ a.firstmonth(); } } }
but when press button more exceptions can bear count. ideas?
sorry if basic question.
edit, here exceptions when click button:
exception in thread "awt-eventqueue-0" java.lang.numberformatexception: null @ java.lang.integer.parseint(integer.java:454) @ java.lang.integer.parseint(integer.java:527) @ pkg110_term3.getdata.firstmonth(getdata.java:75) @ pkg110_term3.userinterface$myhandler.actionperformed(userinterface.java:71) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2018) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2341) @ 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:6505) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3321) @ java.awt.component.processevent(component.java:6270) @ java.awt.container.processevent(container.java:2229) @ java.awt.component.dispatcheventimpl(component.java:4861) @ java.awt.container.dispatcheventimpl(container.java:2287) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4832) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4492) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4422) @ java.awt.container.dispatcheventimpl(container.java:2273) @ java.awt.window.dispatcheventimpl(window.java:2719) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:729) @ java.awt.eventqueue.access$200(eventqueue.java:103) @ java.awt.eventqueue$3.run(eventqueue.java:688) @ java.awt.eventqueue$3.run(eventqueue.java:686) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:87) @ java.awt.eventqueue$4.run(eventqueue.java:702) @ java.awt.eventqueue$4.run(eventqueue.java:700) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:699) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:242) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:161) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:150) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:146) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:138) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:91)
given integer.parseint takes string
argument, it's safe sorteddata
3d string
array. stacktrace, array sorteddata
initialized individual elements are not.
the default value object
types null
. same applys values within object
array.
make sure these elements have been assigned values prior invoking integer.parseint
.
for (int i=0; < sorteddata.length; i++) { (int j=0; j < sorteddata[i].length; j++) { (int k=0; k < sorteddata[i][j].length; k++) { sorteddata[i][j][k] = ... // string } } }
Comments
Post a Comment