Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.nullpointerexception when using maps with arraylist

 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have posted this question on another site also since I am desperately looking for help in this issue. Im a beginner in Java and I have created the following program, where the operations is class which consists of different fields and acts like a struct in C. The code is as follows:



I get a nullpointexception error on the line for(operations o : obs.get(x)). Can anyone tell me what the problem is?
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That could be because x is a null string.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does obs.get(x) return?
 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x is not a null string. It is the string passed by the user to the execute method. And in this case, I pass in a valid string and it is received correctly too. I have checked.
 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
obs.get(x) is supposed to return a record for stream/funct/funcgroup which corresponds to the x i.e name
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preet Rams wrote:obs.get(x) is supposed to return a record for stream/funct/funcgroup which corresponds to the x i.e name


The question isn't what is it supposed to return. The question is what does it return? ... add a debugging statement to confirm.

Henry
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preet Rams, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/77349-java-lang-nullpointerexception-when-using-maps-arraylist.html
 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It returns null.. But when I print out the Map entries, I can see that the key and value fields are perfect like I expect it to be
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do an "equals" comparison between the value you're trying to look up and all the keys - apparently none of the keys passes the "equals" test. This should be easy to figure out once you do this.
 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf for the reply! I did an equals test like this and it returns true. But this was inside the for loop, how do I do an equals check outside it? And I read somewhere now that I need to implement equals(Object) and hashcode for the Hashmap...for it to identify in which object it should search for x. http://stackoverflow.com/questions/6078207/why-does-this-hashmap-get-return-a-null How do I do this?
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still a bit confused on how you got the code to compile.

Preet Rams wrote:


The obs instance is a Map that takes an ArrayList<String> as the key. You use a String instance, x, as the key. How come the compiler is not complaining?

Henry
 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry I understand your doubt...but how else can I assign a list of strings as the key?? I get no compiler errors for this program..just that the get method returns null.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preet Rams wrote:@Henry I understand your doubt...but how else can I assign a list of strings as the key??

Why would you want to? A key is supposed to be specific.

Suggestion: Instead of telling us what you've done, why not tell us what you want.
That way we don't have to guess the intent of code that doesn't work.

Also:
  • Classes should start with capital letters, so it should be Operations, not operations.
  • Your field names appear to be using some sort of weird Hungarian Notation. My advice: forget it. You really don't need it in Java.

  • I get no compiler errors for this program..just that the get method returns null.

    Then you plainly haven't given us what you compiled, because
    op.functii = functi.get(i);
    won't work because there is nothing called 'functi' - at least in what you've shown us.

    Winston
     
    Preet Rams
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston..Thanks for the reply. I found my mistake. 1. Yes I was assigning a list to the key, which was wrong.

    2. I was not assigning an instance of my class as input to the value, but an array instance of my class. So I corrected both and it is working fine now. Thanks for your time and help.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic