aspose file tools*
The moose likes Beginning Java and the fly likes java.lang.nullpointerexception when using maps with arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.nullpointerexception when using maps with arraylist" Watch "java.lang.nullpointerexception when using maps with arraylist" New topic
Author

java.lang.nullpointerexception when using maps with arraylist

Preet Rams
Greenhorn

Joined: May 27, 2013
Posts: 25
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

That could be because x is a null string.


SCJP, SCWCD.
|Asking Good Questions|
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42293
    
  64
What does obs.get(x) return?


Ping & DNS - my free Android networking tools app
Preet Rams
Greenhorn

Joined: May 27, 2013
Posts: 25
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

Joined: May 27, 2013
Posts: 25
obs.get(x) is supposed to return a record for stream/funct/funcgroup which corresponds to the x i.e name
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Preet Rams, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/77349-java-lang-nullpointerexception-when-using-maps-arraylist.html


luck, db
There are no new questions, but there may be new answers.
Preet Rams
Greenhorn

Joined: May 27, 2013
Posts: 25
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
Marshal

Joined: Mar 22, 2005
Posts: 42293
    
  64
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

Joined: May 27, 2013
Posts: 25
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: May 27, 2013
Posts: 25
@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

Joined: Mar 17, 2011
Posts: 8016
    
  22

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

    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Preet Rams
    Greenhorn

    Joined: May 27, 2013
    Posts: 25
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: java.lang.nullpointerexception when using maps with arraylist