wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes null pointer exception in Game Of Life Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null pointer exception in Game Of Life " Watch "null pointer exception in Game Of Life " New topic
Author

null pointer exception in Game Of Life

akila sekaran
Ranch Hand

Joined: Jun 12, 2012
Posts: 48

---------------------------------------------------------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

Joined: Jan 10, 2002
Posts: 60053
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
akila sekaran
Ranch Hand

Joined: Jun 12, 2012
Posts: 48
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

Joined: Jan 10, 2002
Posts: 60053
    
  65

How did you know that you had a null pointer exception?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

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

Joined: Jun 12, 2012
Posts: 48
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

Joined: Jan 10, 2002
Posts: 60053
    
  65

Yes. Be sure to UseCodeTags.
Wes McClintock
Greenhorn

Joined: Jul 19, 2011
Posts: 25
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

Joined: Jun 12, 2012
Posts: 48
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

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

Joined: Jan 10, 2002
Posts: 60053
    
  65

akila sekaran wrote:--------------------i get null pointer exception------------------




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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Jun 12, 2012
Posts: 48


This one is the complete program
akila sekaran
Ranch Hand

Joined: Jun 12, 2012
Posts: 48
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

Joined: Jun 12, 2012
Posts: 48
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
akila sekaran
Ranch Hand

Joined: Jun 12, 2012
Posts: 48
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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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

Joined: Jun 12, 2012
Posts: 48
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

Joined: Aug 23, 2010
Posts: 14
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

Joined: Jun 12, 2012
Posts: 48
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null pointer exception in Game Of Life
 
Similar Threads
why javaw file io dose not work properly
Pattern matching an email address read from a file
exception EmptyFileException is never thrown in body of corresponding try statement
plz delete
File Letter Counter help