0

I am experimenting with the multithreading in java and I have a nullPointerException. I try to pass a parameter in my thread and i missed up somewhere but i dunno where. I hope someone can point me where i was stupid :). I wrote this class :

public class MyThread extends Thread{
 private String myBelovedParam = null;

public MyThread (String param){
    myBelovedParam = param;
}
public void run(){
    try {
        AnotherClass.anotherFunction(myBelovedParam );

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}}

And I called it like that :

Thread myThread = new MyThread (theParameterIWant);
    myThread.start();

Thanks all and have a good day !

I edit my post (i change the name of everything and i forgot to change some. that why the name of some thread was different).

Here is the error i get in my console :

Exception in thread "Thread-7" java.lang.NullPointerException
at play.classloading.enhancers.ControllersEnhancer$ControllerInstrumentation.isActionCallAllowed(ControllersEnhancer.java:187)
at anotherFunction(AnotherClass.java)
at threads.myThread.run(myThread.java:21)

I tried to change null by empty String -> no Effect When i debug i can see clearly that theParameterIWant is not null.

pipouloukou
  • 63
  • 1
  • 1
  • 5
  • 1
    Read the error message. It'll tell you where the NPE occurs. – jsheeran Feb 07 '18 at 10:03
  • 1
    what happens if you intialize `myBelovedParam` with an empty string instead of null? also maybe `theParameterIWant` is null – XtremeBaumer Feb 07 '18 at 10:04
  • Some misguiding codes are there: why purgeThread.start() instead of myThread.start(). Another point you are passing the parameter to anotherfunction where you might using by taking the reference of the parameter where might throw null. Please provide the AnotherClass.AnotherFunction detail as well. Please keep a note: function name should start with lowercase. – Arindam Feb 07 '18 at 10:07
  • <> This just worked fine for me. – Suresh Feb 07 '18 at 10:13

0 Answers0