MIKE PERETZ
MIKE PERETZ

Reputation: 159

Java calling array from another class

I am trying to store an arrayList of objects to a text file created from the array ipvalue[] which is instantiated in another class. My problem is that whenever i try to load the array from the Mainframe class to the Arraystorage class, it displays the entire array as null outputs. I guess the problem is that the array data is generated within the Mainframe method, and when I call the array from another class it simply takes the array as it is initialized. So my question is how do I how can I access the array after it is assigned value rather then when it is initialized.

new String[50] 
mainframe class:



public class Mainframe extends javax.swing.JFrame {
static int ipcounter = 0;
public static String[] ipvalue = new String [50];


/**
 * String a[]Creates new form Mainframe
 */
    public Mainframe() {
         try {
        initComponents();
        this.setLocation(500,250);
        Nettest.main(null);
        Display.setText(Nettest.indicator);
        Arraystorage.main(null);
        Path path = Paths.get("ipcounter.txt");

         File file = new File("ipcounter.txt");


         FileReader in = new FileReader("ipcounter.txt");
        BufferedReader br = new BufferedReader(in);

        readline = Integer.parseInt(br.readLine()); 
        in.close();
        ipcounter = readline;

         if(ip.tracker.Nettest.status == true){
         ipvalue[ipcounter] = Readipfromurl.inputLine;
       //  crosscheck();

        Writer writer = null;
         writer = new BufferedWriter(new OutputStreamWriter(
         new FileOutputStream("ipcounter.txt"), "utf-8"));
          writer.write(String.valueOf(ipcounter+1));

          writer.close();
         System.out.println(ipvalue[ipcounter]);
          ipcounter++;


arraystorage class:


public class Arraystorage implements Serializable{
   public static void main(String[] args) throws 
 ClassNotFoundException { 

   ArrayList<String> list = new ArrayList<String>();
  for (int i=0; i<50;i++){
  list.add(Mainframe.ipvalue[i]);}
   try {
       PrintWriter out = new PrintWriter(new BufferedWriter(new 
  FileWriter("data.txt")));
   for( int x = 0; x < list.size(); x++)
 {
 out.println(list.get(x));
 }

   out.close();


   } catch (IOException ex) {

 Logger.getLogger(Arraystorage.class.getName()).log(Level.SEVERE, 
null, ex);
   }

Upvotes: 1

Views: 2536

Answers (3)

MIKE PERETZ
MIKE PERETZ

Reputation: 159

you guys are probably right but i found another solution in the mainframe class i call the main method of arraystorage Arraystorage.main(null);
after i assign values to the array and it works!

Upvotes: 1

NastyDiaper
NastyDiaper

Reputation: 2558

Create a static block initializer:

public class Mainframe extends javax.swing.JFrame {

    static int ipcounter = 0;
    public static String[] ipvalue = new String [50];

    static {
        File file = new File("ipcounter.txt");
        FileReader in = new FileReader("ipcounter.txt");
        BufferedReader br = new BufferedReader(in);

        // Fill your ipvalue

        in.close();
    }

    // The rest of your class definition
    public MainFrame() {

    }

Upvotes: 1

Ivan Pronin
Ivan Pronin

Reputation: 1896

In short, you are not calling Mainframe() that fills your array with data. You should do it in the main method before you need to use ipvalue array. So, just call this method on the Mainframe instance:

public static void main(String[] args) throws  ClassNotFoundException { 

  ArrayList<String> list = new ArrayList<String>();
  Mainframe frame = new Mainframe();
  frame.Mainframe();
  for (int i=0; i<50;i++){
  list.add(Mainframe.ipvalue[i]);
  }
  ... // do the rest

In long, please, read at least Oracle Java tutorial for Java basics for naming conventions, code structure and many more

Upvotes: 1

Related Questions