Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: New programmer, basic error help!

  1. #1
    Junior Member Robertgif's Avatar
    Join Date
    Feb 2013
    Posts
    19
    My Mood
    Amazed
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default New programmer, basic error help!

    Hello all! I JUST started coding java and I was just practicing and I came across an error:

    Exception in thread "main" java.lang.NullPointerException
    at fightGame.startFight(fightGame.java:26)
    at launchGame.main(launchGame.java:8)


    Soooo... I'm making a stab here and guessing that the error is on line 26 of my fightGame method. Ill post that below:

    public class fightGame {
    	player fighter1;
    	player fighter2;
     
     public void introFight() {
    	player fighter1 = new player();
    	player fighter2 = new player();
     
    	fighter1.sex = 'M';
    	fighter2.sex = 'M';
     
    	fighter1.name = "Lig";
    	fighter2.name = "Bone";
     
    	fighter1.health = 100;
    	fighter2.health = 100;
     
    	System.out.println("Welcome one and all to the grand fighting arena!");
    	System.out.println("Today we will see the fight off between the one and only: " + fighter1.name + " and " + fighter2.name);
    	System.out.println("\n");
     }
     public void startFight() {
    	while ((fighter1.health > 0) | (fighter2.health > 0)) { 	
    	fighter1.attack();
    	fighter2.attack();
    	System.out.println(fighter1.name + "'s health is now: " + fighter1.health);
    	System.out.println(fighter2.name + "'s health is now: " + fighter2.health);
      }	
    	if (fighter1.health <= 0){
    	fighter1.die();
    	}
    	else{
    	fighter2.die();
    	}
     }
    }

    I just can't figure out what is wrong with that line of code, so any help or tips or suggestions would be awesome!

    Sorry if it is a real obvious problem!

    Thanks so much!!!

    --- Update ---

    *Woops! Meant in my startFight method!
    "You should not let technology (or method) drive your design, but make your design drive the technology".

    Always learning!


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: New programmer, basic error help!

    .

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: New programmer, basic error help!

    Hi,

    I couldn't understand your problem clearly... You post your full code or you post player class. Because i couldn't got null values on that code
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  4. #4
    Junior Member Robertgif's Avatar
    Join Date
    Feb 2013
    Posts
    19
    My Mood
    Amazed
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: New programmer, basic error help!

    Okay sorry, Ill post the other two classes that are involved in the program:

    Here is the player class which I don't believe has an error in it:
    public class player{
    	int health;
    	char sex;
    	String name;
    	short damage = 10;
    	int hitchance;
     
     public void attack(){
    	//First decides if they hit or not...
    	hitchance = (int) (Math.random() * 10);
    	if (hitchance >= 4){
    	System.out.println(name + " prepares for an attack...");
    	System.out.println(name + " unleashes the attack for " + damage);
    	health = health - damage;
        }
    	else{
    	System.out.println(name + " missed!");
    	}
     } 
     public void die(){
    	System.out.println(name + " has died");
    	System.out.println(name + " loses");
     }
    }

    And here is the coding for my main class:
    public class launchGame{
     public static void main(String[] args){
    	fightGame round1 = new fightGame();
    	round1.introFight();
    	round1.startFight();
     }
    }
    "You should not let technology (or method) drive your design, but make your design drive the technology".

    Always learning!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: New programmer, basic error help!

    Exception in thread "main" java.lang.NullPointerException
    at fightGame.startFight(fightGame.java:26)
    At line 26 in fightGame there was a variable with a null value when that statement was executed. Find the variable with the null value and backtrack in the code to see why that variable didn't have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member Robertgif's Avatar
    Join Date
    Feb 2013
    Posts
    19
    My Mood
    Amazed
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: New programmer, basic error help!

    Quote Originally Posted by Norm View Post
    At line 26 in fightGame there was a variable with a null value when that statement was executed. Find the variable with the null value and backtrack in the code to see why that variable didn't have a valid value.
    Thank you Norm, but I've read this when I was researching the problem before I made the original post and I can't figure out what is wrong with the line of code or why it has a null value.

    The specific line of code is:
    while ((fighter1.health > 0) | (fighter2.health > 0)) {

    But...
    If you look just above that I have already declared that both of the object's health are 100. I believe they are also declared in the player class too. There is nothing between those two lines of code that would cause the value to go null. I think
    "You should not let technology (or method) drive your design, but make your design drive the technology".

    Always learning!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: New programmer, basic error help!

    What variable has the null value? If you can't tell by looking, use a println statement to print out the values of the variables so you can see. Add the println immediately before where the error happens.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Robertgif (February 26th, 2013)

  9. #8
    Junior Member Robertgif's Avatar
    Join Date
    Feb 2013
    Posts
    19
    My Mood
    Amazed
    Thanks
    6
    Thanked 1 Time in 1 Post

    Unhappy Re: New programmer, basic error help!

    Alright, thanks for the suggestions.

    So this was my original code:
     public void startFight() {
    	while (fighter1.health > 0 & fighter2.health > 0) { 	
    	fighter1.attack();
    	fighter2.attack();
    	System.out.println(fighter1.name + "'s health is now: " + fighter1.health);
    	System.out.println(fighter2.name + "'s health is now: " + fighter2.health);
      }	
    	if (fighter1.health <= 0){
    	fighter1.die();
    	}
    	else{
    	fighter2.die();
    	}
     }



    This is my new code:
    public void startFight() {
    	System.out.println(fighter1.health + "" + fighter2.health);//<<<<<New line of code!!!!
    	while (fighter1.health > 0 & fighter2.health > 0) { 	
    	fighter1.attack();
    	fighter2.attack();
    	System.out.println(fighter1.name + "'s health is now: " + fighter1.health);
    	System.out.println(fighter2.name + "'s health is now: " + fighter2.health);
      }	
    	if (fighter1.health <= 0){
    	fighter1.die();
    	}
    	else{
    	fighter2.die();
    	}
     }

    The output I'm getting looks like this:
    Welcome one and all to the grand fighting arena!
    Today we will see the fight off between the one and only: Lig and Bone

    Exception in thread "main" java.lang.NullPointerException
    at fightGame.startFight(fightGame.java:23)
    at launchGame.main(launchGame.java:8)


    So it does not display the variables for me. The vairables fighter1.health and fighter2.health were the ones I originally thought were having the problem going into the while loop. But now the error is being thrown here:
    System.out.println(fighter1.health + " " + fighter2.health);

    I put the exact same line of code at the end of the method before this and the output is 100 100 which is what it should be. For some reason when ever it goes into the second method, the startFight() method, it scews up.

    Here is the main class:

    public class launchGame{
     public static void main(String[] args){
    	fightGame round1 = new fightGame();
    	round1.introFight();
    	round1.startFight();
     }
    }

    Could it be a syntax error?

    The game launches and the intro fight method is launched, all is fine there. Now when it goes to run the startFight method, there is a problem with what ever the first line of code is.

    My introFight() method is in post#1

    I'm real new at java so the chances of me doing a syntax error or just leaving something out that I need is really high.
    Last edited by Robertgif; February 26th, 2013 at 04:19 PM. Reason: Trying to offer a little more help
    "You should not let technology (or method) drive your design, but make your design drive the technology".

    Always learning!

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: New programmer, basic error help!

    Exception in thread "main" java.lang.NullPointerException
    at fightGame.startFight(fightGame.java:23)
    Same question as before, now for line 23. What variable is null?

    This code ( fighter1.health) uses two variables:
    fighter1
    and
    fighter1.health

    health is an int which can NOT have a null value.

    only the first one being null would give the NullPointerException.

    Could it be a syntax error?
    No. A syntax error is found by the compiler. A NPE happens at execution.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Robertgif (February 26th, 2013)

  12. #10
    Junior Member Robertgif's Avatar
    Join Date
    Feb 2013
    Posts
    19
    My Mood
    Amazed
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: New programmer, basic error help!

    Alright! Thanks so much!

    I was getting rather frustrated so I went to go do some general reading on java and it kind of hit me like an epiphany.

    I was declaring my player objects inside of the introFight() method which meant that I could either declare the new objects in the startFight() method or I could just declare them as instance variables. I think that is an effective way of explaining it. Thanks so much Norm!
    "You should not let technology (or method) drive your design, but make your design drive the technology".

    Always learning!

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: New programmer, basic error help!

    That error is called shadowing: having a local variable with the same name as a class variable.
    I don't know why IDEs don't give a warning when it is done. I can't think of a time that you would want to do that.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #12
    Junior Member Robertgif's Avatar
    Join Date
    Feb 2013
    Posts
    19
    My Mood
    Amazed
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: New programmer, basic error help!

    While i'm still learning I have just been using the command prompt to run everything, help me understand how the coding really works, but thanks!
    "You should not let technology (or method) drive your design, but make your design drive the technology".

    Always learning!

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: New programmer, basic error help!

    Many of us think starting with the command prompt is the best way.
    Try writing some classes in packages and using them to see how to use classpath.
    Just simple "Hello world" classes but with different packages and the folder structures and classpath settings that are needed to get it to work.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: August 17th, 2013, 07:55 PM
  2. error with "Scanner" class in basic calculator program.
    By wheezebeez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 09:49 AM
  3. Blackjack programming error (programmer is new to programming)
    By JSingh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 09:13 PM
  4. Need Help, Not sure how to fix basic error!
    By ChicoTheMan94 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2012, 01:41 PM

Tags for this Thread