Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8128189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:47:56+00:00 2026-06-06T07:47:56+00:00

I’m having a problem where I receive this error: Exception in thread main java.lang.NullPointerException

  • 0

I’m having a problem where I receive this error:

Exception in thread “main” java.lang.NullPointerException

at com.noxia.Main.startCombat(Main.java:101)

at com.noxia.Area1.createArea1Enemy(Area1.java:43)

at com.noxia.Main.main(Main.java:30)

I know that I need to initialize the variables because they are null, but I can’t seem to figure out what I need to put where. I’ve minimized the code to show just the relevant parts as there are many other variables and methods left out, but this seems to pertain to the issue. Any help would be greatly appreciated =)

public class Main {
    Player p;
    Enemy e;
    Area1 a1;        

    public static void main(String[] args) {
        Main main = new Main();
        main.a1 = new Area1();            
        main.p = new Player(100);
        //the line directly below this is line 30 where the error occurs
        main.a1.createArea1Enemy(10);
    }

    public void startCombat()
    {
        //the line directly below this is line 101 where the error occurs       
        while (p.getCurrentLife() > 0 & a1.e.getLife() > 0)
        {
            p.playerAttack();
            if (p.getCurrentLife() > 0 & a1.e.getLife() > 0)
            {
                e.enemyAttack();
            }
        }   
    }


public class Player extends Main {
    private int currentLife;

    public int getCurrentLife()
    {
        return currentLife;
    }
    public void setCurrentLife(int cl)
    {
        currentLife = cl;
    }

    public Player(int cl)
    {
        currentLife = cl;
    }


public class Enemy extends Main {
    private int life;

    public int getLife()
    {
        return life;
    }
    public void setLife(int lf)
    {
        life = lf;
    }

    public Enemy (inf lf)
    {
        life = lf;
    }


public class Area1 extends Main {

    public void createArea1Enemy(int enemyCounter)
    {
        while (enemyCounter > 0)
        {
            String[] enemyList = {"Enemy1", "Enemy2"} //code for Enemy2 left out below
            int enemyListLength = enemyList.length; 
            int randomEnemy = (int) (Math.random() * enemyListLength);

            if (enemyList[randomEnemy].equals("Enemy1"))
            {
                Enemy enemy1 = new Enemy("Enemy1", 100);
                //the line directly below this is line 43 where the error occurs
                startCombat();
            }
        enemyCounter--;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:47:59+00:00Added an answer on June 6, 2026 at 7:47 am

    The simple answer is that you have to set e to an enemy before calling startCombat.

    But a better way to do this would be to remove e, and pass the enemy object to startCombat using a method parameter. The e field is conceptually wrong. To understand the wrongness, try to come up with a coherent explanation of what it means in terms of the state of a Main object.


    Clearly this is beginner code … and there are a number of bad things about what you have written:

    • The fields of a class should be for object state, not for passing parameter values to methods.
    • You should avoid writing code that accesses the innards of a class … like your main method does.
    • Best practice is to make fields private, and defined getter and / or setter methods (as required) for external classes to access / modify them.
    • You need to learn how to write constructors with parameters.
    • You need to design your code properly. Everything extending Main means that there is going to be no rational model of what the objects “mean”. And there’s the problem that each instance of Enemy and Area1 will have their own copies of the p, e, and a1 fields, and a whole bunch of inappropriate methods.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.