简体   繁体   中英

How to get a string variable from another class

There are 2 main in the 2-class:

  • The class New_String is used to read text file from my file.
  • The class Write is used to show the read value S1(=ar[1]) from the New_String class.

However, no matter how I tried, the Write class is only showing null ,or it throws a NullPointerException error.

Because the program has further function in my next stage, I can't just combine the 2 class in one. Please tell me how to adjust.

write

public class write
{
    //public static String getar=get.ar[1];
    //getar = get.ar[];
   public static void main(String args[]) throws IOException
   {
     New_string file = new New_string();
     //site.readline
     System.out.println(file.S1);
     //String S1 = ar[1];

   }
}

New_string

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class New_string
{
    public static String S1;
    public static int a=0;
    public static String ar[];
    public static int lnum=0;
    public static String line=null;
    public static void main(String args[]) throws IOException
    {
     FileReader fr= new FileReader("read_listflow.txt");
     BufferedReader br=new BufferedReader(fr);

     while ((line=br.readLine())!=null)
     {
         lnum=lnum+1;
     }

     FileReader fr1= new FileReader("read_listflow.txt");
     BufferedReader br1=new BufferedReader(fr1);
     ar=new String[lnum];

     while ((line=br1.readLine())!=null)
     {
         ar[a]=line;
         a=a+1;
     }

     S1 = ar[1];
     }
}

main method will not automatically be called when you instantiate a class. You need to explicitly call the method of class to get the work done.

You have to do something like this..

public class NewString
{
    public static String S1;
    public static int a=0;
    public static String ar[];
    public static int lnum=0;
    public static String line=null;

    public String read() throws IOException
    {
    FileReader fr= new FileReader("read_listflow.txt");
    BufferedReader br=new BufferedReader(fr);
    while ((line=br.readLine())!=null)
    {
        lnum=lnum+1;
    }

    FileReader fr1= new FileReader("read_listflow.txt");
    BufferedReader br1=new BufferedReader(fr1);
    ar=new String[lnum];
    while ((line=br1.readLine())!=null)
    {
        ar[a]=line;
        a=a+1;
    }
    S1 = ar[1];
    return S1;
    }

}

and then

public class Write
{

   public static void main(String args[]) throws IOException
   {
     New_string file = new New_string();

     System.out.println(file.read());
   }
}

Also, please follow Java Naming conventions

Simplest solution is to add a method read in your New_string class and call that method in your write class I have done the changes

public class write
    {
        //public static String getar=get.ar[1];
        //getar = get.ar[];
    public static void main(String args[]) throws IOException
       {
        New_string file = new New_string();
          file.read()
         //site.readline
         System.out.println(file.S1);
         //String S1 = ar[1];

       }
    }

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class New_string
{
    public static String S1;
    public static int a=0;
    public static String ar[];
    public static int lnum=0;
    public static String line=null;
public read(String args[]) throws IOException
   {
     FileReader fr= new FileReader("read_listflow.txt");
     BufferedReader br=new BufferedReader(fr);
     while ((line=br.readLine())!=null)
     {
     lnum=lnum+1;
     }

     FileReader fr1= new FileReader("read_listflow.txt");
     BufferedReader br1=new BufferedReader(fr1);
     ar=new String[lnum];
     while ((line=br1.readLine())!=null)
     {
     ar[a]=line;
     a=a+1;
     }
     S1 = ar[1];
     }
}

Bad code, but the following just does your dirty job :)

public class write
{

    public static void main(String args[]) throws IOException  {
        New_string.main(mull);
        System.out.println(New_string.S1);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM