1 Replies - 265 Views - Last Post: 26 February 2011 - 01:09 PM Rate Topic: -----

#1 deathmeat  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 25-August 10

inheritence

Posted 26 February 2011 - 12:02 PM

hi!,i want to use the function from class second the function getData
basically this is what the program does
class Main has a function getName which returns the field Name.
now class Second has a function named displayData which displays the field Name from class Main
the error i get is:
Exception in thread "main" java.lang.NullPointerException
at javaapplication2.second.displayData(second.java:10)
at javaapplication2.Main.main(Main.java:29)
Java Result: 1
Main d1 = new Main();
public static void displayData()
{
System.out.println(d1.getName());

}

package javaapplication2;

import javaapplication2.second;
public class Main extends second{

   
    private static String name="soroush";
    public static void main(String[] args) {

        second d = new second();
        // TODO code application logic here

        d.displayData();
    //   s1.displayData();



    }

    public void setData(String ne){

        this.name = ne;
    }
    public String getName(){

        return name;
    }

}




package javaapplication2;


import javaapplication2.Main;
public class second {
private static Main m1;
       public static void  displayData(){

           System.out.println(m1.getName());
       }
}



thanks for your time

Is This A Good Question/Topic? 0
  • +

Replies To: inheritence

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10558
  • View blog
  • Posts: 39,064
  • Joined: 27-December 08

Re: inheritence

Posted 26 February 2011 - 01:09 PM

The super class should know nothing about the subclasses. Therefore, there should be no instance of Main in the second class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1