Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Null pointer  RSS feed

 
Wayne Styles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
I am really new to this and have been given some code to enter and then run.

However, it does not do what it is supposed to.

So I cant continue as I cant see the problem.
Can anyone help. Thanks in advance
Here the code, basic and simple

package Admin;

import javax.swing.*;

public class Admin{

static int numberOfStaff;
static String[]staffNames;
public Admin(){
}
public static void main(String[] args){
Admin.setThingsUp();
System.out.println("Goodbye");
System.exit(0);
}
private static void setThingsUp(){
String userln=JOptionPane.showInputDialog("Enter the number of Nurses presently employed.");
numberOfStaff=Integer.parseInt(userln);
for (int count = 0 ; count < numberOfStaff; count=count + 1)
{
staffNames[count]=JOptionPane.showInputDialog("Enter the name of the next Nurse.");
}
}
}
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your loop, you must initiate the array prior to referencing in. In other words, you reference staffNames[0], staffNames[1], [2], etc, etc without initializing the array. The array is not initialized and therefore it is NULL.

The following will correct this:

numberOfStaff=Integer.parseInt(userln);

staffNames = new String[numberOfStaff]; //this added to initialize the array

for (int count = 0 ; count < numberOfStaff; count=count + 1)
{
staffNames[count]=JOptionPane.showInputDialog("Enter the name of the next Nurse.");
}
 
Wayne Styles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Brian.

This was a great help.
 
Wayne Styles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
As you can see Brian gave me a solution, but now when I enter any number of nurses I get the call "goodbye"

Any ideas anyone
 
Wayne Styles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
OK its working now, thanks Brian
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!