Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer exception in Game Of Life

 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

---------------------------------------------------------the above code is in main method------------The code below belongs to Game class--------------------


--------------------i get null pointer exception------------------
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akila sekaran wrote:--------------------i get null pointer exception------------------

Don't tell us, show us!

Post the stack trace. Be sure to UseCodeTags.

And please, lose all the needless dashes. They just make you post harder to read.
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
akila sekaran wrote:--------------------i get null pointer exception------------------

Don't tell us, show us!

Post the stack trace. Be sure to UseCodeTags.

And please, lose all the needless dashes. They just make you post harder to read.


how do i get the stack trace?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you know that you had a null pointer exception?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be on your console, Cut and Paste.

If you are redirecting or piping output to a file, could be there also.

We have no idea where you are getting this exception, nor can guess from the code snippet.
Help us to help you.


WP
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:How did you know that you had a null pointer exception?


by output obviously.. so you mean to post the output here ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Be sure to UseCodeTags.
 
Wes McClintock
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find that whenever I get null pointer exceptions, I find its almost always because I'm trying to access an element in an array that doesn't exist. For example, using array[5] when they array is only 5 elements long (meaning it would only go up to array[4]).
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wes McClintock wrote:I find that whenever I get null pointer exceptions, I find its almost always because I'm trying to access an element in an array that doesn't exist. For example, using array[5] when they array is only 5 elements long (meaning it would only go up to array[4]).

That would result in an array out of bounds exception, not an NPE.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akila sekaran wrote:--------------------i get null pointer exception------------------


java.util.InputMismatchException


Not an null pointer exception at all, is it? Please be accurate when positing. Otherwise, you waste your own time, as well as the time of anyone trying to help you.

DId you look up the javadoc for java.util.InputMismatchException? (⇐ hint: click on it)
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akila sekaran wrote:

So in fact, then, it's not a NullPointerException at all?

The InputMismatchException is caused by trying to read an int from the Scanner when there isn't an int to read. Note that you check the next token is an int, but then you try and read two ints in a row. You need to look at what the input actually is.
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This one is the complete program
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
akila sekaran wrote:

So in fact, then, it's not a NullPointerException at all?

The InputMismatchException is caused by trying to read an int from the Scanner when there isn't an int to read. Note that you check the next token is an int, but then you try and read two ints in a row. You need to look at what the input actually is.


i got a NPE before but now when i executed i got this IME. Could you see the code and temme where i went wrong?
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
akila sekaran wrote:

So in fact, then, it's not a NullPointerException at all?

The InputMismatchException is caused by trying to read an int from the Scanner when there isn't an int to read. Note that you check the next token is an int, but then you try and read two ints in a row. You need to look at what the input actually is.
This is the error that i iam getting . Sorry abt that Input Mismatch Exception.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akila sekaran wrote:i got a NPE before but now when i executed i got this IME. Could you see the code and temme where i went wrong?


I already mentioned that your input doesn't match the data you're trying to read.

Mind you, there are other problems with your code as well. One thing I noticed is that you haven't initialised the member variables arr or arr1 anywhere. You might think you have, but in the constructor you've actually declared local variables and initialised those instead. I suspect that might be the cause of the NullPointerException you got.

Edit: I think that is the cause. I'm not certain, though, because the code you've posted doesn't quite match up with the line numbers in the exception trace. Which line is actually line 60 of your original code?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's kind of hard to tell you since:

1) You didn't post the exact and complete error you got
2) We don't know how to run this program

When you post a question here, your primary responsibility is to make it as easy as possible for someone to help you. I tried to run your program, but it is asking for a file name. I don't know what that file should be. Is it an input file, or is it for output? If it is input, what should be in it?

So If I wanted to help you, now I have to spend MORE of my time trying to figure out how to run this, which gives me less time to actually solve the problem. Many folks won't even bother.

So, please give us all the relevant information, and make it easy for us to help you, and folks will. Make it hard, and people will try but most likely give up.
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:It's kind of hard to tell you since:

1) You didn't post the exact and complete error you got
2) We don't know how to run this program

When you post a question here, your primary responsibility is to make it as easy as possible for someone to help you. I tried to run your program, but it is asking for a file name. I don't know what that file should be. Is it an input file, or is it for output? If it is input, what should be in it?

So If I wanted to help you, now I have to spend MORE of my time trying to figure out how to run this, which gives me less time to actually solve the problem. Many folks won't even bother.

So, please give us all the relevant information, and make it easy for us to help you, and folks will. Make it hard, and people will try but most likely give up.



oh forgive me.. i am totally new to forum way of handling things.. thats why i missed to mention about input file.. it is similar to the one given.. and the program basically takes in the first two numbers of the file to form a matrix kind of answer based on a few conditions..
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akila sekaran wrote:
oh forgive me.. i am totally new to forum way of handling things..

It's not a big deal - provided you get better. I was trying to give you some suggestions, but there is more info on our HowToAskQuestionsOnJavaRanch FAQ (click the underlined part). It gives some useful tips on how we work and how to get the best possible experience here.
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
akila sekaran wrote:
oh forgive me.. i am totally new to forum way of handling things..

It's not a big deal - provided you get better. I was trying to give you some suggestions, but there is more info on our HowToAskQuestionsOnJavaRanch FAQ (click the underlined part). It gives some useful tips on how we work and how to get the best possible experience here.


ok.. by the way how do we dynamically mention the sizes for array?
 
N Tiwa
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using arrayList as you don't need to specify the size and it grows dynamically. You can add an element to it just like you do with an array. You can find more on this here- http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
 
akila sekaran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Tiwa wrote:You should be using arrayList as you don't need to specify the size and it grows dynamically. You can add an element to it just like you do with an array. You can find more on this here- http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html


yeah i used arraylist.. thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic