Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Code Challenge: Get Data From Web (Android Track)

In task 2, I failed but I don't know why Here is the question: Inside onCreate(), declare a URL variable named "treehouseUrl" and initialize it with the URL constructor, using the "URL" member variable as the parameter. Put this line inside a "try" block and then catch a MalformedURLException named "e". Don't do anything in the "catch" block yet.


Here is the original code provided

package com.example;

import android.os.Bundle;
import android.view.View;
import java.net.MalformedURLException;
import android.util.Log;

public class MainListActivity extends ListActivity {
    public static final String URL = "teamtreehouse.com";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_list);

        // Add Task 2 code here
    }
}

Here is my solution

package com.example;

import android.os.Bundle;
import android.view.View;
import java.net.MalformedURLException;
import android.util.Log;

public class MainListActivity extends ListActivity {
    public static final String URL = "teamtreehouse.com";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_list);

        try {
            java.net.URL treehouseUrl = new java.net.URL(URL); 
        } catch(MalformedURLException e) {}
        // Add Task 2 code here
    }
}

Here is the error message Bummer! Don't forget to initialize "treehouseUrl" with the "URL(String url)" constructor.


Apparently, the tester doesn't find my initialization. I also tried import java.net.URL and do URL treehouseUrl = new URL(URL); it results in the same error. Meanwhile, it makes me uncomfortable defining a static String with a name conflicting with the java.net.URL class.

Any help will be appreciated.

2 Answers

Answer is

        try {
            URL treehouseUrl = new URL(URL);
        } catch(MalformedURLException e) {
        }

And we don't need to import java.net.URL;

The answer is

try {
    URL treehouseUrl = new URL(URL);
} 
    catch(MalformedURLException e) {
}

How it fits into the rest of the project looks like this

package com.example;

import android.os.Bundle;
import android.view.View;
import java.net.MalformedURLException;
import android.util.Log;



public class MainListActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_list);

        // Add Task 2 code here
        try {
            URL treehouseUrl = new URL(URL);
        } 
        catch(MalformedURLException e) {
        }

    }
  public static final String URL ="teamtreehouse.com";
}