Jump to content
  • Advertisement
Sign in to follow this  
Ronnie TRFC

Help with java NullPointerException Needed!

This topic is 3955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm having trouble with the java.lang.NullPointerException runtime error. I'm new to java programming and hope someone may be able to help. I get the error when i attempt to delete a element from a list. Here are two portions of the code that i believe are contributing to the error:
// Here i delete element 0 off the list if conditions are met.

if (command == word1Command) {
// write pre-action user code here

// write post-action user code here
if (stringWord1.getLayout() ==(Item.LAYOUT_CENTER))
{
stringWord1.setLayout(Item.LAYOUT_LEFT);
wordList.delete(0); // <---- This is when i get the error
tPlayer1++;
oPlayer1++;
}
}

// Here's the code for the list which comes after the previous code segment


public List getWordList() {
if (wordList == null) {
// write pre-init user code here
wordList = new List("Ataddin", Choice.IMPLICIT);
wordList.append(Word1, null);
wordList.append(Word2, null);
wordList.append(Word3, null);
wordList.append(Word4, null);
wordList.append(Word5, null);
wordList.append(Word6, null);
wordList.append(Word7, null);
wordList.append(Word8, null);
wordList.append(Word9, null);
wordList.append(Word10, null);
wordList.setCommandListener(this);
wordList.setSelectedFlags(new boolean[] { false, false, false, false, false, false, false, false, false, false });
// write post-init user code here
}
return wordList;
}

Thanks in advance and any questions just ask as i'm getting so frustrated at the mo. I'm using Netbeans IDE 6 and J2ME. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
So, what does netbeans tell you is null? If you debug it, setting a breakpoint at the exception you should be able to highlight bits of code or look at the locals to see if wordList is null or wordList is empty, which is the cause of your problems. If you need to, you can set the breakpoint earlier and step through the code to see where things aren't going according to plan.

Or, if that makes no sense how far do you get before you're confused about debugging?

Share this post


Link to post
Share on other sites
Thank you for the reply.

I hope this answers your question:

If i comment out 'wordList.delete(0);' then it runs fine, so i believe this is were the problem is.

Does that help in any way?

Share this post


Link to post
Share on other sites
Well sort of. It means your initial diagnosis about what line it is on is correct; otherwise I'd just have to take your word for it. It doesn't let me/us know how far you got into using the debugger so we can help you learn that valuable skill.

Share this post


Link to post
Share on other sites
I have just set a breakpoint at the point in the code where it throws the error (wordList.delete(0);).

I then hovered over wordList and it says it equals null.

I presume this is the problem, how would i go about fixing this.

Again thanks for your help, i appreciate it.

Share this post


Link to post
Share on other sites
Make it not delete while null. How to do that is largely dependent on the circumstances of your code at that point. In some cases, you should just not delete; in some cases it's a bug elsewhere (since it should never be null there by design, you just made a mistake somewhere) and needs to be fixed; in some cases it's a runtime error and the exception should be thrown (and perhaps a handler added to deal with it gracefully).

I don't know without more info about the context. And you're welcome :]

Share this post


Link to post
Share on other sites
i will try to put it into context for you as i'm struggling to understand the error.

On screen i have 10 words these are stringItems, i then have a command for each of these being:


if (command == word1Command) {
// write pre-action user code here

// write post-action user code here
if (stringWord1.getLayout() ==(Item.LAYOUT_CENTER))
{
stringWord1.setLayout(Item.LAYOUT_LEFT);
wordList.delete(0);
tPlayer1++;
oPlayer1++;
}
}



I have one of these commands for each stringItem. I also have a list which is on a screen the the user cannot access, it is not lined to anything. This list contains the 10 words. So when the user selects the word (stringItem) the command should delete the word from the of screen list as well as do other irrelevant things.

Does this help in any way and can you see a possible reason for the error?

I hope i haven't overcomplicated things, if so please say as i would love to sort this out.

Thank You.

Share this post


Link to post
Share on other sites
You haven't overcomplicated things, but the snippet you're supplying is incomplete, and thus leaves much of the context out such as how wordList is being (and supposed to be) populated. I suspect that it is a problem populating the list, but the snippet you provided in the original post that should be creating it looks kosher at first glance, guessing on a lot of the stuff. So how's it being populated/created?

Share this post


Link to post
Share on other sites
Due to the fact i have been using J2ME and the pallette to drag items onto the screen for design alot of the code is generated in this way. i think this is where the list is created and poputlated:


public List getWordList() {
if (wordList == null) {
// write pre-init user code here
wordList = new List("Ataddin", Choice.IMPLICIT);
wordList.append(Word1, null);
wordList.append(Word2, null);
wordList.append(Word3, null);
wordList.append(Word4, null);
wordList.append(Word5, null);
wordList.append(Word6, null);
wordList.append(Word7, null);
wordList.append(Word8, null);
wordList.append(Word9, null);
wordList.append(Word10, null);
wordList.setCommandListener(this);
wordList.setSelectedFlags(new boolean[] { false, false, false, false, false, false, false, false, false, false });
// write post-init user code here
}
return wordList;
}



Word1, Word2 .....etc being variable declared at the top of my program like so:


String Word1 = "to";
String Word2 = "tip";
String Word3 = "pods";
String Word4 = "on";
String Word5 = "pan";
String Word6 = "is";
String Word7 = "as";
String Word8 = "a";
String Word9 = "tad";
String Word10 = "din";



I would upload the entire code for the program but it i quite large, but if that would help i'm more than happy to do so.

Thanks again.

Share this post


Link to post
Share on other sites
And wordList is a member of the class containing getWordList or is wordList being assigned the result of getWordList? I imagine the former? And where is that getWordList called in relation to your exception throwing delete?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!