• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot figure out why I am getting a NullPointerException  RSS feed

 
Hitarth Patel
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This throws an exception when I write the name of the file. I do not know what is causing it.

I am calling the file like this..this is in main, in the TestHashing class.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A number of things that are not clear in your post:
you wrote:This throws an exception when I write the name of the file.

I am calling the file like this...


The first "This" is very vague and non-specific. What is "this" that throws an exception. Yes, we know it's your code, but which line? What is the exception message you're seeing and which line does it refer to?

"when I write the name of the file" ... what do you mean by that? Did you mean when you type it in and hit Enter after getting prompted for a file name?

The second "like this" is also vague... so is "the file".  Which file are you referring to? Do you mean the program that you're running? You showed a second snippet but there's no apparent connection between the second snippet and the first snippet. It's not clear how program execution would flow from either one of the snippets you showed to the other.

When you post a question, you have to remember that we are not clairvoyant, we can't see what you did, don't know what you are thinking, don't know much about your situation besides what you choose to share. When you leave out important details that would help give us a better context, then it's difficult to help you.
 
Hitarth Patel
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize for my mistakes. I tried my best to be thorough.
My intellij idea does not want to give me a stacktrace for this even after I did "e.printStack();". I found that when I wrote the contains method in my ArrayList class, is where the error is at. I think it may be logic, but it looks right to me.
The exception message I am seeing is " java.lang.NullPointerException" and nothing else.

Yes, to the latter, when I type in my text files name("test.txt", without the quotations) and hit Enter after getting prompted for a file name.

Some new things I found, I may have written my contains method wrong for my ArrayList<AnyType>.

This is my contains method. I am trying to not override the equals() method. I want to be able to check whether the arraylist has the object in it and return true or false based on where it found it or not. This was my best attempt...
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still haven't given enough information to allow anyone else to connect the dots. You have given a third code snippet that has no apparent connection to either of the previous two disconnected snippets you have provided. You need to help us connect the dots, otherwise, we'll be as confused as you are, except it doesn't matter to us because we can just ignore you and let you figure it out yourself. Help us help you; give us a more context.
 
Hitarth Patel
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so, basically, I was calling my contains method in my TestHashing class, and since my logic was off it returned a nullpointerexception. The second snippet is me asking for the file and instantiating a few variables. The first snippet is where the error happens, but its not really happening there, its happening in the contains method. So fixing the contains method fixed my problem. Thank you, for your time.

Is there a guide I could look at to make sure I am thorough enough when asking questions on here? Thank you!!
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hitarth Patel wrote:Is there a guide I could look at to make sure I am thorough enough when asking questions on here? Thank you!!

If you look at the signature in my replies, you'll see a link that says "How to Ask Questions..."  You might want to start there to see what you can do to make it easy for others to help you.

Basically, the best way to help others help you is to give them as much relevant information about your problem as you can. Exactly how much information that is depends on each different situation. It could be as simple as "I'm getting a NullPointerException and the error message seems to indicate that it's this statement: System.out.println(myList.something())" -- if this were the case, it's easy. The thing that is null is the myList variable.

It isn't always that easy though and there are other situations, such as yours, where more information is needed to give a good idea of the context in which the problem is happening. Try to make it easy to connect all the information you give. If you give a lot of information but don't give a way to make the connections between them all, then it's going to be more confusing.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, you said "I was calling my contains method in my TestHashing class..." but you never show any code that lets other people see exactly how you're calling that method. How do you expect us to know how you coded that call up? You want us to make assumptions or imagine how you did it? We're not going to do that.  Be specific. Be detailed.  If you're going to be a programmer, you need to learn how to communicate your problems properly, with the proper "exhibits" like you were in a court of law trying to prove something, or like you were a detective trying to analyze a crime scene. 

You continue by writing, "... and since my logic was off it returned a nullpointerexception."

Your "logic was off"? How do you know your logic was off? How do you know it had something to do with your logic? More importantly, how do you expect other people to understand that statement if you don't help them understand how you came to that conclusion?  You have to explain, step-by-step, with references to code that you have shared with us, how you came to conclude that "your logic was off".

Also, it helps if you use correct terminology. A NullPointerException is thrown, not "returned" as you said.  So, it makes more sense if you say "When I called blah blah, a NullPointerException was thrown somewhere... I'm not really sure which line of code threw that exception but it seems to happen after the call to the XXX() method is made.  Here's the code where I call the XXX() method and here's the code for the XXX() method."
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!