-3

Basically I create a custom object in one program and instantiate with values in one program, then I create another program and want to print the instantiated object output.

import java.util.Scanner;
class example {
    int value;
    String name;
    String city;
}
public class Yummy21 {
    static example[] obj=new example[3];
    static Scanner input=new Scanner(System.in);
    public static void main(String args[])
    {
        init();
    }
    public static void init()
    {
        for(int i=0;i<3;i++)
        {
            obj[i]=new example();
        }
        for(int i=0;i<3;i++)
        {
            System.out.println("for the object "+i+" enter the name ");
            obj[i].name=input.next();
            System.out.println("for the object "+i+" enter the city ");
            obj[i].city=input.next();
            System.out.println("for the object "+i+" enter the name ");
            obj[i].value=input.nextInt();
        }
    }
}

And the next program:

public class Yummy22 extends Yummy21 {
    public static void main(String args[])
    {
        for(int j=0;j<3;j++)
        {
            System.out.println("the value of the object["+j+"].name is "+obj[j].name);
        }
    }
}

The first program works fine, meaning it takes the values and the second program shows NullPointerException.

Whymarrh
  • 13,139
  • 14
  • 57
  • 108
Creative_Cimmons
  • 255
  • 1
  • 2
  • 11
  • How exactly do you plan on passing data between these two programs? – Powerlord Mar 19 '14 at 19:49
  • Did you consider that `obj` is null? Why? Because you never initialized it. In what way does your code constitute two programs? – Elliott Frisch Mar 19 '14 at 19:53
  • 1
    When you run Yummy21, the data is created then thrown out when the program ends. That program has nothing to do with the execution of Yummy22. If you need to keep the data generated by Yummy21, then you need to save it to some kind of long term storage, like a file, as Tyson Moncrief suggested. – dorr Mar 19 '14 at 20:05
  • thanks @dorr, i guess that is the only solution !!! – Creative_Cimmons Mar 19 '14 at 20:09

2 Answers2

3

Yummy22.main does not invoke Yummy21.init. perhaps put a

static { init(); }

in Yummy21.

This being said, these 3 classes are abominations of Java. Please at least follow naming conventions. Class example should be Example. Also better to code all this with object level fields, constructors and non static members

Kirby
  • 15,127
  • 10
  • 89
  • 104
  • @Krib ythanks, but plz note that this a very simple version of what i am actually implementing and i don't want to invoke init() and run the whole thing again. I GOTTA BE COMPLETELY MODULAR – Creative_Cimmons Mar 19 '14 at 19:50
  • 3
    If this isn't the code that you're having problems with, then please post that code. – Kirby Mar 19 '14 at 19:51
  • the real one runs upto 700 lines and bit math involved – Creative_Cimmons Mar 19 '14 at 19:53
  • pgm1 init()-takes value from the user, calculalte() perform computation pgm2 uses javafx to display the output. so now you see why the instantiated object need to be passed. @Kirby – Creative_Cimmons Mar 19 '14 at 19:54
0

serialize the data from program 1 and write it to disk and then read it from program 2.

Tyson Moncrief
  • 526
  • 3
  • 10
  • I considered using databases, or even passing them as a string, but any easier and more accurate method @Tyson Moncrief – Creative_Cimmons Mar 19 '14 at 19:59
  • 1
    not sure what you mean by a more accurate method. The inheritance model you have set up really buys you nothing. When you execute program 1 and program 2, they are executing within separate domains are are not sharing resources at all. Once the last line of program 1 completes, the values stored in obj are gone. You have to persist the data somehow and writing to a file seems to the be the easiest approach. – Tyson Moncrief Mar 19 '14 at 20:08