Skip to main content

A null pointer exception?

1 reply [Last post]
Joined: 2013-02-09

Hi! ! (^_^) Im sort of new to java and recently got an error that i have no idea how to fix. Below is a simplified version of the code I am trying to print out, instead of doing what I wanted it to do... I got a null pointer exception....ugh (@_@) ! . The weird thing is that I did the same thing earlier with : myLeagues[i].getAge() and it worked perfectly, does anyone have some ideas of what might be going wrong or what I can do to fix it. Thanks....

for(int i=0;i String temp = myLeagues[i].getName();

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2013-02-09

Most of the time, a null pointer exceptions occures when you try to use a reference to an object when it has not been initialized yet.

In this case, the myLeagues[i] array location might not have been initialized with the object that has the getName() method. Initialized meaning something like:

myLeagues[i] = new League();