Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

java.lang.nullpointerexception when using maps with arraylist

 
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?
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That could be because x is a null string.
 
Rancher
Posts: 43011
76
  • 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
 
author
Posts: 23855
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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: 43011
76
  • 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
Posts: 23855
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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.
     
    What's wrong? Where are you going? Stop! Read this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!