3

My problem is simple: I need to access variable history (which is declared in class BinaryServer) from another class.I'm using more classes to run this code.It's just simple client and server made of sockets.Client sends to server binary code/text and server translates it to text/binary code and sends it back to client.I can provide all classes if needed.

BinaryServer class

import java.net.*;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import graphics.gui;

public class BinaryServer extends gui implements ActionListener,Runnable
{
private ServerSocket server;
private Socket client;
public String text;
private BufferedReader reader;
public static ArrayList<String> history;


public static String binary_letter;
public static String[] letter;
public static int i;
public static String[] binary;
public static String sendback;


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

   gui.buildframe(310,360,"Binary translator server");
   gui.buildpane(300,300,true);
   gui.buildbutton(300,20,"Translate");
   instance.server(63400);
}

public void server(int port)throws IOException
{
    history=new ArrayList<String>(100);
    server=new ServerSocket(port);
    button.addActionListener(this);

    while(true)
    {
        client=server.accept();
        reader=new BufferedReader(new InputStreamReader(client.getInputStream()));
        text=reader.readLine();
        history.add(text);
        message.setText(message.getText()+"\n"+text+": ");
    }
}

@Override
public void actionPerformed(ActionEvent e)
{
    Thread response=new Thread(new BinaryServer());

    if(text.contains("0"))
    {
        int length=text.length();
        letter=new String[length+1];
        sendback="";
        int begin=-8;
        int end=0;

        for(i=1;i<=length/8;i++)
        {
            begin=begin+8;
            end=i*8;
            binary_letter=text.substring(begin,end);
            Libary.translate();
            message.setText(message.getText()+letter[i]);
            sendback=sendback+letter[0+i];
        }
    }
    else
    {
        int length=text.length();
        letter=new String[length+1];
        binary=new String[length+1];
        sendback="";

        for(i=1;i<=length;i++)
        {
            letter[i]=text.substring(i-1,i);
            Libary.encode();
            message.setText(message.getText()+binary[i]);
            sendback=sendback+binary[0+i];
        }
    }
    response.start();
}

public void run()
{
    try
    {
    Socket feedback=new Socket("localhost",63403);

    PrintWriter writer=new PrintWriter(feedback.getOutputStream(),true);
    writer.println(sendback);
    feedback.close();
    return;
    }
    catch(IOException exc)
    {
        System.out.println("");
    }
}
}

BinaryHistory class (The one I want access variable from)

public class BinaryHistory
{
    public static void main(String[] args) 
    {
        show();
    }
    public static void show()
    {
        System.out.println(BinaryServer.history);
}

When I access variable history from class BinaryHistory, it's alway null.

4
  • do you call server in anywhere before access? server method seems to initialize it Commented Feb 15, 2014 at 19:47
  • i see 2 mains here, and there is no istanstiation of the server class. Commented Feb 15, 2014 at 19:47
  • I'm working in eclipse and it's in same enviroment.Even after extending class BinaryServer, variable is still null. Commented Feb 15, 2014 at 19:48
  • @nr4bt he calls it in main instance.server(63400); Commented Feb 15, 2014 at 19:49

1 Answer 1

1

If you only declare the variable, regardless of the type or whether or not it's static, it will get a default value of null.

You have to initialize the variable too:

public static ArrayList<String> history = new ArrayList<>();
Sign up to request clarification or add additional context in comments.

4 Comments

There's line history=new ArrayList<String>(100); in code.Isn't it initialization? (in method server)
Not in a static context, no. If you reassign it later in a method, then its value is assigned when the method is called.
Is there need to delete mentioned line?
It's always better to initialize fields at their declaration point and not to separate the declaration and initialization because you only insert unnecessary complexity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.