0

Getting

"Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException"

at soms1.actionPerformed over all ActionListener events of lines:

 thread.set_state(1);
 thread.set_state(2);
 thread.reset;
 thread.send;

Can you please suggest where is null values is getting in thread? this code worked fine previously but after little changes in code this error showed up.

Code Snippet:

public class soms1 extends JApplet {

static public SOM_thread1 thread = null;
static JApplet japplet = new JApplet();

static JButton go1 = new JButton("go");
static JButton play = new JButton("play");
static JButton reset = new JButton("Reset");
static JButton send = new JButton("send");

static Container c;
static JPanel p1 = new JPanel();
static JPanel p2 = new JPanel();
static JPanel p6 = new JPanel();

Toolkit tool;

public void setup_applet() {
    japplet.setVisible(true);

    go1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {thread.set_state(1);}
    });
    play.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {thread.set_state(2);}
    });
    reset.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {thread.reset();}
    });
    send.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {thread.send();}
    });
}

public void init() {
    tool = Toolkit.getDefaultToolkit();
    setup_applet();
    setup_radios();
    setup_layout();

    thread = new SOM_thread1();
    thread.init();
    thread.start();
    if (clientSocket != null) {thread.set_state(2);}
   }
}

class SOM_thread1 extends Thread{
public void set_state(int f) {state = f;}
public void send() { //code block}
public void reset() {//code block}
public void run() {//code block}
public void init() {reset();}
}

As i am newbie in java please suggest changes thank you in advance.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
APL
  • 21
  • 6
  • 1
    `public class soms1 extends JApplet {` .. *"i am newbie in java"* Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Mar 26 '16 at 07:38
  • 1
    Well, that code does not even compile here, so unable to test it, I'll move to the next question.. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 26 '16 at 07:40
  • Have you instantiated "thread"? Is it this statement "thread = new SOM_thread1();" anywhere to be seen in your code? – RubioRic Mar 26 '16 at 07:41
  • @RubioRic yes "thread = new SOM_thread1();" statement is used in init function of soms1 class. as it passes clientsocket to SOM_thread1 i have to initialize it in init – APL Mar 26 '16 at 07:48
  • @APL My mistake. I didn't see it. – RubioRic Mar 26 '16 at 07:51
  • @Andrew this code is very large so i put minimized code over here for understanding purpose. this is part of my college project – APL Mar 26 '16 at 07:53
  • BTW - 1) `static JApplet japplet = new JApplet();` I've coded many applets and never had to declare an applet within the running applet. Why do you think you need to? 2) `static JButton go1 = new JButton("go");` When it comes to GUIs. `static` delcarations are more often a source of problems, than a solution of problems. Why do you think those attributes need to be declared `static`? – Andrew Thompson Mar 26 '16 at 07:54
  • 1
    try instantiating "thread" before calling setup_applet() – Phileo99 Mar 26 '16 at 07:57
  • *"so i put minimized code over here"* Yes, that is the first part of [mcve] - now try to attain the other parts of it. Especially read the SSCCE document, in which I go into more detail, and offer tips for trimming code. What you possibly did not realize is that on SO we have a close reason for questions that explicitly mentions 'no MCVE'. So far it has one (of 5) votes. Unless you post code we can test before 4 more people vote - the question will be closed. – Andrew Thompson Mar 26 '16 at 07:57
  • Do you have these lines in one line like this? public void send() { //code block} the end block character '}' is commented – mantlabs Mar 26 '16 at 07:58

1 Answers1

0

You are calling setup_applet before initializing the thread inside the init function.

Replace this

public void init() { 
     tool = Toolkit.getDefaultToolkit();
     setup_applet();
     setup_radios();
     setup_layout();
     thread = new SOM_thread1(); 
     thread.init(); 
     thread.start();
     if (clientSocket != null) {
          thread.set_state(2);
           }
       } 
  } 

With this

public void init() { 
     tool = Toolkit.getDefaultToolkit();
     thread = new SOM_thread1();
     setup_applet();
     setup_radios();
     setup_layout(); 
     thread.init(); 
     thread.start();
     if (clientSocket != null) {
          thread.set_state(2);
           }
       } 

(Note also the removed } in the end of the init function. You have one more)

Also separate the } in your thread methods to another line, because you are commenting it with //

mantlabs
  • 352
  • 2
  • 6