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 6927991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:07:34+00:00 2026-05-27T11:07:34+00:00

I have a class called ‘Items’ to which ‘Equips’ extends from and ‘Helmet’ then

  • 0

I have a class called ‘Items’ to which ‘Equips’ extends from and ‘Helmet’ then extends from ‘Equips’. I have a method called ‘getStats’ that loads the item’s stats from a .txt file. If I put the ‘getStats’ method in the ‘Items’ class, whatever field I try to access in a ‘Helmet’ object using ‘this.’ shows up null. The field I’m trying to access in ‘Helmet’ is initialized when the helmet is created before the text file is loaded. I could very easily just put the ‘getStats’ method in the ‘Equips’ class and put a blank ‘getStats’ method in the ‘Items’ class, but I was wondering if there was a way to make it work how it is. Thanks in advance!

Items.java:

package com.projects.aoa;

import static com.projects.aoa.Print.*;

import java.util.*;
import java.io.*;

class Items {

String name, type;
int id;
int hp, mp, str, def;
boolean vacent;

static void getAllStats(Items[] e){
    for(Items i : e){
        getItemStats(i);
    }
}

static void getItemStats(Items i){
    i.getStats();
}

void getStats(){
    try {
        //System.out.println(System.getProperty("user.dir"));

        print(this.name); //THIS shows up as null as well as those \/below\/
        FileInputStream fstream = new FileInputStream(System.getProperty("user.dir") 
                + "/src/com/projects/aoa/" + this.type + this.name + ".txt");

        DataInputStream in = new DataInputStream(fstream);

        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        String line;
        int counter = 0;

        while ((line = br.readLine()) != null) {
            if (line.length() == 0){
                break;
            }

            switch (counter) {
            case 0:
                this.hp = Integer.parseInt(line);
                counter++;
                break;
            case 1:
                this.mp = Integer.parseInt(line);
                counter++;
                break;
            case 2:
                this.def = Integer.parseInt(line);
                counter++;
                break;
            case 3:
                this.str = Integer.parseInt(line);
                counter++;
                break;
            }   
        }


        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    } 
}

Game.java:

Helmet headBand = new Helmet("HeadBand");
Helmet bronzeHelmet = new Helmet("BronzeHelmet");


    Items[] equips = {
            headBand, bronzeHelmet
    };



    getAllStats(equips);

Equips.java:

  package com.projects.aoa;

import static com.projects.aoa.Print.print;
import static com.projects.aoa.Print.println;

import java.io.*;



class Equips extends Items{
    String name, type;
    int hp, mp, str, def;




    void printStats(){
        println("[" + name + "]");
        println("Type: " + type);
        println("HP:  " + hp);
        println("MP:  " + mp);
        println("Def: " + def);
        println("Str: " + str);
    }
}

class Helmet extends Equips {
    Helmet(String name){
        this.name = name;
        this.type = "h_";
    }
}
  • 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-05-27T11:07:34+00:00Added an answer on May 27, 2026 at 11:07 am

    You haven’t shown us your Helmet class, so it’s hard to say what’s going on – but my guess is that you’re redeclaring fields with the same name in Helmet. Those will hide the fields in Items, whereas you really just want to use the fields from Items.

    So here’s a short but complete example which demonstrates what I think is going on:

    class SuperClass {
        String name;
    
        public void setName(String newName) {
            // This writes to the field in SuperClass
            name = newName;
        }
    }
    
    class SubClass extends SuperClass {
        // This *hides* the field in SuperClass
        String name;
    
        public void showName() {
            // This reads the field from SubClass, which
            // nothing writes to...
            System.out.println("My name is " + name);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            SubClass x = new SubClass();
            x.setName("Test");
            x.showName();
        }
    }
    

    I would recommend that:

    • You make all fields private, writing properties to give access to other classes as required
    • You get rid of the fields in Helmet which hide the ones in Items
    • You change your class names to avoid the plurality – Item and Equipment instead of Items

    Here’s a fixed version of the above code:

    class SuperClass {
        private String name;
    
        public void setName(String newName) {
            name = newName;
        }
    
        public String getName() {
            return name;
        }
    }
    
    class SubClass extends SuperClass {
        public void showName() {
            System.out.println("My name is " + getName());
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            SubClass x = new SubClass();
            x.setName("Test");
            x.showName();
        }
    }
    

    (Obviously you also need to think about what access to put on the properties etc, but that’s a separate matter.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called EventConsumer which defines an event EventConsumed and a method
I have a class called Projectiles which inherits from CCSprite class, Currently there are
I have a class called ItemBase from which a number of classes inherit. In
I have a class called Profile that has some simple properties and then it
I have a class called BaseB which can be called from A . I
I have a class called Ship and a class called Lifeboat Lifeboat inherits from
I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way
I have a class called User with static function loginRequired(), which returns false if
I have a class called Path for which there are defined about 10 methods,
I have a class called UserInfo that contains details about a given user. There

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.