1

Following this question on SO I've run into a problem. This is the JSON I'm trying to parse:

{
    "origin": "XX.XX.XXX.XXX"
}

Here are the important parts of my three main classes:

DDNS.java

public static void main(String[] args) throws Exception {
    Net.returnIp();
    System.out.println(Data.origin);
}

Net.java

static void returnIp() throws UnsupportedEncodingException, MalformedURLException, IOException {
    //Ommited code, all it does is do a GET request to get response
    String responseString = new Scanner(response,"UTF-8").useDelimiter("\\A").next();

    Data data = new Gson().fromJson(responseString, Data.class);  
}

Data.java

public class Data {
    public String origin;

    @Override
    public String toString() {
        return String.format("ip:%s", origin);
    }
}

Unless I overlooked something in the answer, I'm not completely sure what I'm doing wrong, although I have a rough idea. Could someone please explain why this is happening? Thanks.

Community
  • 1
  • 1
Novicode
  • 275
  • 2
  • 3
  • 12
  • 1
    `origin` isn't `static`. – awksp Jul 17 '14 at 22:16
  • @user3580294 Yeah, I could get that far, but I'm extremely new to Java and don't fully understand static objects. I can't just declare it static, so how would I fix it? Thanks you for your input. – Novicode Jul 17 '14 at 22:18
  • What azurefrog said. If a class member isn't `static`, you need to access it through an instance of your class. So you're going to need to get a `Data` object from somewhere. – awksp Jul 17 '14 at 22:21
  • @azurefrog Could you provide an example an an answer? As a really new user to Java, this is already over my head. Looks like I have some more learning to do. – Novicode Jul 17 '14 at 22:22
  • I've converted my comments into an answer so I'd have enough room to explain things. – azurefrog Jul 17 '14 at 22:33
  • @Novicode - Read the Answers to the linked Question. It has examples. – Stephen C Jul 17 '14 at 22:33

1 Answers1

2

The problem is that you are trying to access a non-static member of Data in your main method, without having actually a reference to a Data object.

The easiest way I can think of to fix this is to change Net#returnIp() to return the Data it creates when parsing the json:

//Net.java
static Data returnIp() throws UnsupportedEncodingException, MalformedURLException, IOException {
    //Ommited code, all it does is do a GET request to get response
    String responseString = new Scanner(response,"UTF-8").useDelimiter("\\A").next();

    return new Gson().fromJson(responseString, Data.class);  
}

Then you can use the Data object you have created:

//DDNS.java
public static void main(String[] args) throws Exception {
    Data data = Net.returnIp();
    System.out.println(data);  // this will automatically call data's toString() and format its origin
}

The concepts of Objects and Classes are very fundamental to the Java language. You will need to understand them to get very far.

I would recommend reading oracle's Object-Oriented Programming Concepts, starting with the "What is an Object?" and "What is a Class?" links.

azurefrog
  • 10,785
  • 7
  • 42
  • 56