0

This is my abstract class

I can't print fields inside abstract class from the display method in subclass

public abstract  class Account {
    private int cusId;
    private int cusAccountid;
    private String cusName;

    Account(int cusId, int cusAccountid,String cusName){
        this.cusId=cusId;
        this.cusName=cusName;
        this.cusAccountid=cusAccountid;             
    } 

    public int getCusId() {
        return cusId;
    }

    public int getCusAccountid() {
        return cusAccountid;
    }

    public String getCusName() {
        return cusName;
    }

    abstract public void display();    
}

I just extend this abstract class(account) from (Savings class)

public class Savings extends Account{
    private int savId;
    private String  savBranch; 

    Savings(int cusId, int cusAccountid,String cusName,int savId, String savBranch) {
        super(cusId,cusAccountid,cusName);
        this.savId= savId;
        this.savBranch=savBranch;
       }

    @Override
    public void display() {
        Account s = null;
        System.out.println(s.getCusId()+s.getCusAccountid()+s.getCusName()+savId+savBranch);        
    }                
}

And here is my MAIN

public class Bank {

    public static void main(String[] arg){

        Savings sc = new Savings(2,2,"ram",4,"thambaram");
        sc.display();
    }

}

and it shows Runtime error

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at com.trying.Savings.display(Savings.java:22)
    at com.trying.Bank.main(Bank.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
jaga D sh
  • 81
  • 6
  • 3
    Aside from anything else, it's best if you don't *try* to run code that doesn't compile. When the IDE warns you (as I'm pretty sure it would) that your code doesn't compile, and asks you if you're *sure* you want to run it anyway, say *no* and fix the compilation errors. – Jon Skeet Aug 16 '15 at 15:19
  • 3
    You can't access fields of a `null` reference either. – Peter Lawrey Aug 16 '15 at 15:22
  • The code compiles fine. What are you talking about? It's all about the null reference.. – Theo Aug 16 '15 at 15:36

3 Answers3

2

By doing a quick search of your exact error, I found this which suggests that it's a bug in Netbeans.

If you're using Netbeans, go to Build -> Compiling and uncheck Compile on save. Source.

Oh and also, you should probably not be setting Account s = null; and then trying to print values from it.

Community
  • 1
  • 1
Deximus
  • 455
  • 5
  • 8
1

Your problem lies with the code :

Account s = null;

in the method display of the class Savings.

I feel that your main objective is to printout the values of the super class Account fields cusId, cusAccountid, cusName;. If this is correct this can be achieved by calling the public getters you have declared in the Account class. But if you want to access the fields of the current instance, then you should be using this instead of instantiating the new variable s. So instead of using s.getCusId() use this.getCusId() and so on.

Blip
  • 3,061
  • 5
  • 22
  • 50
0

change the display method from the savings class to be like this (you can access it as the modifiers are public):

@Override
public void display() {

    System.out.println(this.getCusId()+" "+this.getCusAccountid()+" "+this.getCusName()+" "+savId+savBranch);

}

You were creating a null Account object and trying to display its fields which is not correct.

Theo
  • 1,165
  • 9
  • 14
  • 1
    You should add an explanation on why ir does not work with his code but works with yours, otherwise your answer will be a copy-paste fix for him – Dici Aug 16 '15 at 15:23
  • Hey dude its working now thanks a lot.. explain me please – jaga D sh Aug 16 '15 at 15:28
  • Hello. I simply removed Account s = null; Because of that you would have had a null object always. Like saying s=new S(2); s.display() and in the display method always making it null. Also you can remove this if you want (it will work regardless). – Theo Aug 16 '15 at 15:33
  • like: System.out.println(getCusId()+" "+getCusAccountid()+" "+getCusName()+" "+savId+savBranch); – Theo Aug 16 '15 at 15:33
  • about the "this" keyword, check this out: http://stackoverflow.com/questions/2429062/java-when-to-use-this-keyword – Theo Aug 16 '15 at 15:34