0

Edit: It's not something as simple as a nullpointerexception being thrown. The thread is working severable times before throwing the nullpointerexception meaning that it suddenly decides to stop working and I can't figure out why

Hello I have the following thread

private class IsCreditAdded implements Runnable{
    ActionEvent evt;

    public IsCreditAdded(ActionEvent evt){
        this.evt = evt;
    }

    @Override
    public void run(){
        while (AddMinutes.creditChosen != true){
        }
        creditAdded(evt, AddMinutes.getEvt()); 
    }
}

It waits for a boolean to change from a GUI class with options to select a value, and then when the boolean changes it executes a method called creditAdded with values evt(passed in from constructor), and a static getMethod which contains the ActionEvent which was selected

public void creditAdded(ActionEvent evt, ActionEvent duration){
    System.out.println(evt.getActionCommand() + " " + duration.getActionCommand());
    switch (evt.getActionCommand()){
        case ("voipCredit"):
            switch (duration.getActionCommand()){
                case ("cancel"):
                    System.out.println("CANCEL");
                    disableButtons(evt, duration);
                    break;
            }
    addMinutes.dispose();
    AddMinutes.reset();
    sessionStorage.printTransActions();
}

Here is where the thread is created:

private void creditButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

    switch (evt.getActionCommand()){
        case ("voipCredit"):
            addMinutes = new AddMinutes();
            addMinutes.setService("VOIP");
            addMinutes.setVisible(true);
            addMinutes.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
            addMinutes.setPrices(nf.format(SessionPrices.getVOIP_10_MINS()), nf.format(SessionPrices.getVOIP_30_MINS()), 
                    nf.format(SessionPrices.getVOIP_1_HOUR()), nf.format(SessionPrices.getVOIP_ALL_DAY()));
            threadVar = new Thread(new IsCreditAdded(evt));
            threadVar.start();  
            disableAllButtons();
            break;
    }
}

Now the problem Im having is whenever I select the button which gives the getActionCommand 'cancel' it works two times, sometimes three, but then if I try it again I get a nullpointerexception. The actual error message I get is:

Exception in thread "Thread-6" java.lang.NullPointerException at com.aidanmoore.InternetCafeGUI.creditAdded(InternetCafeGUI.java:416) at com.aidanmoore.InternetCafeGUI$IsCreditAdded.run(InternetCafeGUI.java:411) at java.lang.Thread.run(Thread.java:745) BUILD STOPPED (total time: 9 seconds)"

I can't figure out why it is throwing this exception after hours of searching for similar problems others have had and since the thread works x amount of times before throwing the exception I Cant work out why it suddenly breaks

bamramjam
  • 19
  • 2
  • 1
    So which one is line 416? – Dawood ibn Kareem Mar 05 '17 at 02:12
  • Exactly. If you studied NPE prior to posting the question, you'd already know how important that line is, and what debugging steps you should do *prior* to posting. – Hovercraft Full Of Eels Mar 05 '17 at 02:13
  • hi It's the System.out.println(evt.getActionCommand() + " " + duration.getActionCommand()); – bamramjam Mar 05 '17 at 02:13
  • .... and after reading the duplicates on this problem... that should tell you which variables need to be checked for null, right? – Hovercraft Full Of Eels Mar 05 '17 at 02:18
  • Side comment: Your use of ActionEvents is atypical/unusual. – Hovercraft Full Of Eels Mar 05 '17 at 02:19
  • yes but the thread works several times before it suddenly changes to nullpointer – bamramjam Mar 05 '17 at 02:20
  • And regarding, `"It's not something as simple as a nullpointerexception being thrown. The thread is working severable times before throwing the nullpointerexception meaning that it suddenly decides to stop working and I can't figure out why..."` -- doubtful that anyone here can guess how it's different or guess at a solution without a valid [mcve]. – Hovercraft Full Of Eels Mar 05 '17 at 02:21
  • Until you post this, your question remains a duplicate. – Hovercraft Full Of Eels Mar 05 '17 at 02:21
  • yeah I was just trying out attaching the same action to several buttons but depending on the actionevent.getActionCommand it would execute differently – bamramjam Mar 05 '17 at 02:22
  • Bottom line: you're here too early as you haven't yet studied NPE's (else you'd have known to post the line reference from the get go) and have not done enough yet to isolate your problem. Shoot, which variable is null when the NPE is thrown? You should be checking this. Use your debugger, use logging -- first isolate the error, and all review associated object states when the NPE is thrown. You've still got work to do. – Hovercraft Full Of Eels Mar 05 '17 at 02:26
  • sorry if I've come across as arrogant, i've been trying to find a solution to this problem for the past 12hours straight and its annoying me – bamramjam Mar 05 '17 at 02:30
  • No, you're not arrogant, but I just don't see your current question as answerable. To change this, you would need (I believe) to create and post a valid [mcve]. – Hovercraft Full Of Eels Mar 05 '17 at 03:05
  • Is it possible that `AddMinutes.getEvt()` would return null? Until you show us more code, all anyone can do is guess. That's why Hovercraft keeps asking for an MCVE. The mystery is why you keep refusing. – Dawood ibn Kareem Mar 05 '17 at 08:19

0 Answers0