Null Pointer Exception

john1992

Honorable
Jan 22, 2014
4
0
10,510
Hello, first post, total noob here. Not even sure if this is the right place to post this.

I get this NullPointerException that I haven't a clue what that means. I have tried googling it and no luck understanding what this error is/does or the cause of it. One thing could be that I don't entirely understand parallel arrays. Help!

Here's my code:
------------------------------------------------------------------------------------------------------------------------------------
public class Lab10D
{
private int[]list=new int[6];
private String[]names;
private int[]age;
private int count;

public static void main(String[]args)
{
Lab10D lab=new Lab10D();
lab.input();
lab.output();
}
public void input()
{
try
{
Scanner reader=new Scanner(new File("lab10d.dat"));
reader.useDelimiter("/\r\n");
while(reader.hasNext())
{
names[count++]=reader.next();
age[count++]=reader.nextInt();
}
}
catch(IOException e)
{
System.out.println(e);
System.exit(0);
}
}
public void output()
{
for(int i=0;i<=count;i++)
{
System.out.println(names+" is "+age+" years old.");
}
}
}
------------------------------------------------------------------------------------------------------------------------------------
This is lab10d.dat

Grace Kelly/28
Bob Hope/42
Frank Lovejoy/32
William Shatner/45
Howdy Doody/12
Lester Scruggs/54
------------------------------------------------------------------------------------------------------------------------------------
And the error message.

Exception in thread "main" java.lang.NullPointerException
at Lab10D.input(Lab10D.java:26)
at Lab10D.main(Lab10D.java:15)


Edit: Firstly, the school year has been over for a month, this isn't homework, I'm just going over everything I did in class this year. And secondly, of course it's bad programming to you, like I said I'm a noob. I just finished my first year of programming.
 
Solution
I think by "parallel arrays" your assignment is simply referring to the fact that you are adding data to two arrays in the same block of code and even at the same index; that is, more or less in parallel (but not really...). This has nothing to do with parallel programming. Don't even bother looking into that now as it's very complex and without a very solid understanding of sequential programming you'll get lost.

A NullPointerException is thrown whenever you try to use an uninitialised variable. Variables are simply friendly names that point to/identify objects in memory (which is why they are also called identifiers). A "value" of null simply means that it points to nothing, at least in Java. You can't do much with nothing except to...

Pinhedd

Distinguished
Moderator
Hi,

We have a no-homework policy, but I'll give you a hint:

Java:
names[count++]=reader.next();
age[count++]=reader.nextInt();

Your problem is here.

Your object Lab10D has four class attributes named list, names, age, and count. Three of them are array references, and one is a scalar integer. One of the array references, list, is automatically initialized to an integer array containing 6 elements, but names, and age are not initialized either during the object's creation or during the object's constructor. As such, they are left uninitialized (Java automatically sets uninitialized references to null).

When you attempt to dereference the names array, it is still null and thus fails.
 

ksham

Honorable
Mar 29, 2013
175
0
10,760
Also a bit of bad programming. If it's only a script made for importing, storing the values into an instance of the class itself is kind of silly. I doubt that class will be used for anything else. Just write it all in main().

Having said that, it may be better to use a Hashtable / HashMap or ArrayList.
 

john1992

Honorable
Jan 22, 2014
4
0
10,510
Thanks for the answers, but I still don't quite understand explaining things program-y like. I don't quite understand parallel programming either. Possibly someone put an example of a solution for parallel programming and explain what you're doing in the solution?
 

ksham

Honorable
Mar 29, 2013
175
0
10,760
It's not parallel programming. You are misusing terminology. It's just a simple straight-forward program.

I won't write the code for you. But I can point you in the right direction. If you use an ArrayList or Hashtable / HashMap, it would make your life easier than a fixed-length array structure.
 

Pinhedd

Distinguished
Moderator


There are many different disciplines for parallel programming at various levels of computation: multi-processing, kernel level multi-threading, user level multi-threading (also called coroutines), explicit and implicit vectorization (data level parallelism), stream processing (a very explicit variation of vectorization that is the building block for GPUs), explicit instruction parallelism, superscalar parallelism, etc...

Some of these are the responsibility of the programmer and must be performed explicitly, some are performed automatically by the compiler (sometimes with programmer hints), some are performed by the compiler to match the machine architecture (Itanium), while others are architectural features that are handled automatically and independently of the compiled code (superscalar execution).

Acutely understanding the difference between just the academic domains of multi-processing, kernel multi-threading, and user multi-threading without getting into OS and hardware specific details would take several weeks of reading and studying. Learning the libraries adds several more. Parallel programming requires a full order of magnitude more consideration than good old fashioned sequential programming, and has two orders of magnitudes more 'gotchas' to be aware of.

You should give yourself a pat on the back for going over your coursework and validating your knowledge, that's a very good habit to get into. However, you should work on learning and mastering the fundamentals before moving up to the hardcore god-tier stuff.
 

randomizer

Distinguished
I think by "parallel arrays" your assignment is simply referring to the fact that you are adding data to two arrays in the same block of code and even at the same index; that is, more or less in parallel (but not really...). This has nothing to do with parallel programming. Don't even bother looking into that now as it's very complex and without a very solid understanding of sequential programming you'll get lost.

A NullPointerException is thrown whenever you try to use an uninitialised variable. Variables are simply friendly names that point to/identify objects in memory (which is why they are also called identifiers). A "value" of null simply means that it points to nothing, at least in Java. You can't do much with nothing except to replace it with something :)

Other languages have more exotic ways of representing nothing (like Ruby, where nothing doesn't really exist), but Java is pretty straight-forward.
 
Solution