0

This is my code in java netbeans. I want to display the username get from another form in label and there is errer ...

 String username1; 
 public agent_menu(String username1)
 {
   this.username1=username1;
   System.out.println("user: "+username1);


    jLabel1.setText(username1);
   }

the error:

user: 22
java.lang.NullPointerException
ms1991
  • 1
  • 2

1 Answers1

0

Your jLabel1 is most likely not initialized. It prints out to the console what you expect then you receive the null pointer exception when you call jLabel1.setText

Dan King
  • 930
  • 1
  • 9
  • 27
  • I tried many times...but still error – ms1991 Dec 05 '15 at 02:45
  • Add jLabel1 = new JLabel() before jLabel1.setText(username1) and try it. It should resolve the specific error but might not give you the desired effect. You might have to provide some more info but resueman link would be a good starting place. – Dan King Dec 05 '15 at 02:53
  • @ms1991 If that doesn't fix it, then the error is not in the code you posted. The only variables which you show are `username1` and `jLabel1`. If neither of them is null, it's something else. – resueman Dec 05 '15 at 03:10
  • but when I use this code System.out.println("user: "+username); the result is user: 22 but in I used labal it came error – ms1991 Dec 05 '15 at 03:16