Results 1 to 8 of 8
  1. #1
    dug201 is offline Member
    Join Date
    Aug 2012
    Location
    London, UK
    Posts
    15
    Rep Power
    0

    Default non-static variable request cannot be referenced from a static context

    I have the below code and I'm not sure what I need to do to call and run my doit() class from my main class.

    When I compile it i errors out with: non-static variable request cannot be referenced from a static context

    Java Code:
    import java.net.*;
    //import java.io.*;
    import javax.net.ssl.*;
    import javax.net.SocketFactory;
    import java.util.*;
    import java.io.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
    import java.net.URLEncoder;
    
    public class encode{
        
    	
        static int port = 80;
        static String hostname = "xxx.xxx.xxx.xxx";
        static List<String> IDs = new ArrayList<String>(); //id for the download
        static List<String> statuses = new ArrayList<String>(); //http status for the download request 200,201,204,400
        static List<String> filenames=new ArrayList<String>();
        static List<String> sites=new ArrayList<String>();
        static String outputFolder = "/xxx/xxxx/xxxx/";
        boolean downloadFailed = false; //if a download fails in the download() function, wait 5 mins then retry
        String outputFolder2 = "/xxxxx/xxxxx/xxxx/";
    	String request = "";
        
         public static void main(String[] args) {
         	    
          try {
                    System.out.println("Pushing data");            
                    String xml = doit(request);
         
            } catch (Exception e) {
                e.printStackTrace();
            }
    
         }
         
      
    public String doit(String XML_IN) {
    
    	String response = "";
    	String filename = "xmlfile_out_" + new Date().getTime() + "_.xml"; 
    		String sendEncoding = "UTF-8"; 
    	
    	HttpURLConnection urlConn = null;
    	OutputStreamWriter out = null; 
    	InputStream in = null; 
    	
    	try {
    	
    	request = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>some xml";
    		
    		URL url = new URL("http://domain.xxx.xxx/xxx/xxxx/"); 
    		urlConn = (HttpURLConnection)url.openConnection(); 
    		urlConn.setRequestMethod("POST"); 
    		urlConn.setDoOutput(true); 
    		urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
    		urlConn.setRequestProperty("Accept","application/xml");
    		urlConn.setRequestProperty("charset","UTF-8");
    		urlConn.connect();
    		
    		out = new OutputStreamWriter(urlConn.getOutputStream());
    		out.write(request);
    		
    		FileOutputStream out2 = new FileOutputStream(outputFolder2 + filename);
    		PrintStream p = new PrintStream(out2);
    		p.println(request);
    		
    		out.flush();
    		out.close();
    			
    		int status = urlConn.getResponseCode();
    		System.out.println("Status code:" + status);
    		
    		if (status!=200) {
    			in = urlConn.getErrorStream();
    		}
    		else {
    			in = urlConn.getInputStream(); 
    		}
    	
    	
    		    InputStreamReader inReader = new InputStreamReader(in, sendEncoding);
    	  	    StringBuffer responseBuffer = new StringBuffer();
    		    char[] buffer = new char[1024]; 
    		
    		    int bytes; 
    	         while ((bytes = inReader.read(buffer)) != -1) { 
    		     	responseBuffer.append(buffer, 0, bytes); 
    		   } 
    		
    		    response = responseBuffer.toString(); 
    		
    		    System.out.println(response);
    		
    		if (status!=200) {    
    		    System.out.println("HTTP status code wasn't 200, so must be something wrong - FAILED");
    		    }
    		} 
    		
    		catch (Exception e) {
    			e.printStackTrace();
    		} 
    		
    		finally { 
    			if (out != null) { 
    				try {
    					out.close();
    				} catch (Exception e) {} 
    			} 
    		if (in != null) { 
    			try {
    					in.close();
    				} catch (Exception e) {
    					e.printStackTrace();
    				} 
    			} 
    		
    		if (urlConn != null) { 
    			urlConn.disconnect(); 
    				} 
    			}
    		
    			return response;
    		
    			}
    	
    	  }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: non-static variable request cannot be referenced from a static context

    Exactly what it says.
    You have several non-static things in there, but you never create an 'encode' object (naming conventions mean that that should be 'Encode'), so they never exist.
    That includes your method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    dug201 is offline Member
    Join Date
    Aug 2012
    Location
    London, UK
    Posts
    15
    Rep Power
    0

    Default Re: non-static variable request cannot be referenced from a static context

    Tolls, I'm new to Java, so apologies in advance if what I say is so basic.

    I have added these two new lines in my main method:
    String xml = new Encode();
    xml = doit(request);

    but still get the same error, i guess I'm still not doiong what you suggested? can you care to provide a more details based on my code?

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: non-static variable request cannot be referenced from a static context

    If you haven't done objects yet, and how to create them, then you need to simply make everything static.
    This is not how Java should be written, but I have no idea what, exactly, you are being taught. It might simply be general programming.

    If you need more information then the best start point is the Java Tutorials over at Oracle.
    Here's the basics tutorial.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    dug201 is offline Member
    Join Date
    Aug 2012
    Location
    London, UK
    Posts
    15
    Rep Power
    0

    Default Re: non-static variable request cannot be referenced from a static context

    I know how to create objects, but clearly not in Java. I would really appricite for someone to just show me something in code.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: non-static variable request cannot be referenced from a static context

    Quote Originally Posted by dug201 View Post
    I know how to create objects, but clearly not in Java. I would really appricite for someone to just show me something in code.
    Too much trouble to go through the tutorial Tolls suggested, is it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: non-static variable request cannot be referenced from a static context

    Encode enc = new Encode(); // i believe that's how we create an object
    String xml = enc.doIt(request);

    If I were you I'd name my class Encode instead of encode,
    I would also name my method doIt instead of doit...

  8. #8
    dug201 is offline Member
    Join Date
    Aug 2012
    Location
    London, UK
    Posts
    15
    Rep Power
    0

    Default Re: non-static variable request cannot be referenced from a static context

    Quote Originally Posted by Kagiso View Post
    Encode enc = new Encode(); // i believe that's how we create an object
    String xml = enc.doIt(request);

    If I were you I'd name my class Encode instead of encode,
    I would also name my method doIt instead of doit...
    Thank You Kagiso, this now complied fine... :)

Similar Threads

  1. Replies: 5
    Last Post: 12-01-2010, 06:43 AM
  2. Replies: 5
    Last Post: 10-15-2010, 04:21 AM
  3. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •