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

I have got an error! Urgent, a bit.

This is the error: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference.
I need to solve this, how do i solve it? What caused it?

I also got this error:

              java.lang.RuntimeException: Unable to instantiate activity                   ComponentInfo{everingeducational.monumentum/everingeducational.monumentum.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.

I also got this error:

              java.lang.RuntimeException: Unable to instantiate activity                   ComponentInfo{everingeducational.monumentum/everingeducational.monumentum.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.

10 Answers

It's a lot easier to help out with specific code.

I believe I helped out with a similar question earlier today. Make sure you are calling setContentView() inside your onCreate() method of your Activity (and be sure to give it the proper xml file).

I hope this helps, if not please post your code.

//The package name package everingeducational.monumentum; //Importing the necessary files for the app to run import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

import java.util.Random; //Declaring the class public class MainActivity extends AppCompatActivity {

@Override
//When created run the script below
protected void onCreate(Bundle savedInstanceState) {
    //Supporting what is above
    super.onCreate(savedInstanceState);
    //Set the view to activity_main.xml
    setContentView(R.layout.activity_main);

}

//Grabbing the TextView washingtonFact final TextView washingtonFact = (TextView) findViewById(R.id.washingtonFact); //Grabbing the Button anotherFact Button anotherFact = (Button) findViewById(R.id.anotherFact); //Setting the OnClickListener View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { //Declaring the array facts String[] facts = { //The facts are written below "The Washington Monument, designed by Robert Mills and eventually completed by Thomas Casey and the U.S. Army Corps of Engineers.", "The Washington Monument was the tallest building in the world upon its completion in 1884.", "The structure was completed in two phases of construction, one private (1848-1854) and one public (1876-1884).", "The Washington Monument was built in the shape of an Egyptian obelisk.", "The monument is 555 feet tall", "The monument was supposed to be 600 feet tall with 100 feet tall columns, according to the Roberts Mills design.", "The monument's construction started in 1848.", "It was very hard to raise money for the monument.", "The monuments very tip, is made out of aluminum.", "There were stones from two-different quarries, that is why the first one-third of the monument is a different color.", "The pyramid or pyramidion on top of the tower is made out of 300-ton marble.", "On the east face of the aluminum is Laus Deo which is Praise be to God is inscribed their.", "At 1:51 p.m. on August 23, 2011, a magnitude 5.8 earthquake struck 90 miles southwest of Washington, shaking the monument.", "The original steam-driven elevator had a trip time of 10+ minutes to the top, was replaced with an electric elevator in 1901", " The monument had a steam-powered elevator that could lift six tons of stone up to a movable 20-foot-tall iron frame."};

        String fact = "";

        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(facts.length);

        fact = facts[randomNumber];

        washingtonFact.setText(fact);

    }
};


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Thanks too debugging Jon Kussmann , I have found that the app crashes between the public class MainActivity and @Override

Thanks too debugging Jon Kussmann , I have found that the app crashes between the public class MainActivity and @Override

Hi Evering,

It is a little difficult to read with the formatting of your post. I do think that your error could be because you are declaring/instantiating your views outside of the onCreate method.

@Override
//When created run the script below
protected void onCreate(Bundle savedInstanceState) {
    //Supporting what is above
    super.onCreate(savedInstanceState);
    //Set the view to activity_main.xml
    setContentView(R.layout.activity_main);

} //Move this bracket below the code that declares your views and listeners

I hope this helps.

My button does not work, any thoughts why?

My button does not work, any thoughts why?

My button does not work, any thoughts why?

This is my code:

//The package name package everingeducational.monumentum; //Importing the necessary files for the app to run import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

import java.util.Random; //Declaring the class public class MainActivity extends AppCompatActivity { //This is where it crashes, but this is default, I so stuck -------------------------------------------------------------------------------------------------------------------------------------- @Override //When created run the script below protected void onCreate(Bundle savedInstanceState) { //Supporting what is above super.onCreate(savedInstanceState); //Set the view to activity_main.xml setContentView(R.layout.activity_main);

//Grabbing the TextView washingtonFact final TextView washingtonFact = (TextView) findViewById(R.id.washingtonFact); //Grabbing the Button anotherFact Button anotherFact = (Button) findViewById(R.id.anotherFact); //Setting the OnClickListener View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { //Declaring the array facts String[] facts = { //The facts are written below "The Washington Monument, designed by Robert Mills and eventually completed by Thomas Casey and the U.S. Army Corps of Engineers.", "The Washington Monument was the tallest building in the world upon its completion in 1884.", "The structure was completed in two phases of construction, one private (1848-1854) and one public (1876-1884).", "The Washington Monument was built in the shape of an Egyptian obelisk.", "The monument is 555 feet tall", "The monument was supposed to be 600 feet tall with 100 feet tall columns, according to the Roberts Mills design.", "The monument's construction started in 1848.", "It was very hard to raise money for the monument.", "The monuments very tip, is made out of aluminum.", "There were stones from two-different quarries, that is why the first one-third of the monument is a different color.", "The pyramid or pyramidion on top of the tower is made out of 300-ton marble.", "On the east face of the aluminum is Laus Deo which is Praise be to God is inscribed their.", "At 1:51 p.m. on August 23, 2011, a magnitude 5.8 earthquake struck 90 miles southwest of Washington, shaking the monument.", "The original steam-driven elevator had a trip time of 10+ minutes to the top, was replaced with an electric elevator in 1901", "The monument had a steam-powered elevator that could lift six tons of stone up to a movable 20-foot-tall iron frame.", "It took $1,187,710 to build the Washington Monument.", "The Washington Monument had three (3) floors", "The plans for the Washington Monument began even before George Washington was elected president.", "The original design (by: Robert Mills) for the monument was way different than what ended up being built", "The monument was once the site of a terrorist attack by a 66-year old Navy veteran on December 8, 1982", "The exterior of the Washington Monument was completed on December 6, 1884.", "The Washington Monument consists of 36,491 blocks", "The Washington Monument weighs 90,854 tons.", "Inside the Washington Monument is an elevator and a 897-step stairway (WOW!).", "Lightning rods at the top protect the Washington Monument from lightning strikes.", "There is an observation deck at 500 feet from the ground.", "Work on the monument was stopped in 1854 (when the monument was only 152 feet tall), after donations dropped off.", "From Memorial Day to Labor Day, the hours are 9AM, From Labor Day to Memorial Day, the hours are 9AM to 5PM.", "The monument stands east to the reflecting pool and the Lincoln Memorial", "Robert Mills said that if they did not follow his plan, the monument would look like a stalk of asparagus.", "When the monument was built, it was the tallest building until the Eiffel Tower was built in 1889", "The monument was under restoration (maintenance) (they were fixing it) in 1999", "At the time, Aluminum was rare and expensive so the aluminum tip was special", "In 2001, their was a temporary visitor screening center to reduce terrorist attacks.", "The marble benches were placed to prevent motor veichles to enter, due to terrorist attacks and the Mayor incident.", "The best part of the monument is that it is free to visit and it is free to get tickets!", "The monument was opened to public on October 9, 1888", "The Washington Monument is not the only statue dedicated to George Washington"};

            String fact = "";

            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(facts.length);

            fact = facts[randomNumber];

            washingtonFact.setText(fact);

        }
    };
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

//The package name package everingeducational.monumentum; //Importing the necessary files for the app to run import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

import java.util.Random; //Declaring the class public class MainActivity extends AppCompatActivity { //This is where it crashes, but this is default, I so stuck -------------------------------------------------------------------------------------------------------------------------------------- @Override //When created run the script below protected void onCreate(Bundle savedInstanceState) { //Supporting what is above super.onCreate(savedInstanceState); //Set the view to activity_main.xml setContentView(R.layout.activity_main);

//Grabbing the TextView washingtonFact final TextView washingtonFact = (TextView) findViewById(R.id.washingtonFact); //Grabbing the Button anotherFact Button anotherFact = (Button) findViewById(R.id.anotherFact); //Setting the OnClickListener View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { //Declaring the array facts String[] facts = { //The facts are written below "The Washington Monument, designed by Robert Mills and eventually completed by Thomas Casey and the U.S. Army Corps of Engineers.", "The Washington Monument was the tallest building in the world upon its completion in 1884.", "The structure was completed in two phases of construction, one private (1848-1854) and one public (1876-1884).", "The Washington Monument was built in the shape of an Egyptian obelisk.", "The monument is 555 feet tall", "The monument was supposed to be 600 feet tall with 100 feet tall columns, according to the Roberts Mills design.", "The monument's construction started in 1848.", "It was very hard to raise money for the monument.", "The monuments very tip, is made out of aluminum.", "There were stones from two-different quarries, that is why the first one-third of the monument is a different color.", "The pyramid or pyramidion on top of the tower is made out of 300-ton marble.", "On the east face of the aluminum is Laus Deo which is Praise be to God is inscribed their.", "At 1:51 p.m. on August 23, 2011, a magnitude 5.8 earthquake struck 90 miles southwest of Washington, shaking the monument.", "The original steam-driven elevator had a trip time of 10+ minutes to the top, was replaced with an electric elevator in 1901", "The monument had a steam-powered elevator that could lift six tons of stone up to a movable 20-foot-tall iron frame.", "It took $1,187,710 to build the Washington Monument.", "The Washington Monument had three (3) floors", "The plans for the Washington Monument began even before George Washington was elected president.", "The original design (by: Robert Mills) for the monument was way different than what ended up being built", "The monument was once the site of a terrorist attack by a 66-year old Navy veteran on December 8, 1982", "The exterior of the Washington Monument was completed on December 6, 1884.", "The Washington Monument consists of 36,491 blocks", "The Washington Monument weighs 90,854 tons.", "Inside the Washington Monument is an elevator and a 897-step stairway (WOW!).", "Lightning rods at the top protect the Washington Monument from lightning strikes.", "There is an observation deck at 500 feet from the ground.", "Work on the monument was stopped in 1854 (when the monument was only 152 feet tall), after donations dropped off.", "From Memorial Day to Labor Day, the hours are 9AM, From Labor Day to Memorial Day, the hours are 9AM to 5PM.", "The monument stands east to the reflecting pool and the Lincoln Memorial", "Robert Mills said that if they did not follow his plan, the monument would look like a stalk of asparagus.", "When the monument was built, it was the tallest building until the Eiffel Tower was built in 1889", "The monument was under restoration (maintenance) (they were fixing it) in 1999", "At the time, Aluminum was rare and expensive so the aluminum tip was special", "In 2001, their was a temporary visitor screening center to reduce terrorist attacks.", "The marble benches were placed to prevent motor veichles to enter, due to terrorist attacks and the Mayor incident.", "The best part of the monument is that it is free to visit and it is free to get tickets!", "The monument was opened to public on October 9, 1888", "The Washington Monument is not the only statue dedicated to George Washington"};

            String fact = "";

            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(facts.length);

            fact = facts[randomNumber];

            washingtonFact.setText(fact);

        }
    };
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Now by button doesnt wrk

//The package name package everingeducational.monumentum; //Importing the necessary files for the app to run import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

import java.util.Random; //Declaring the class public class MainActivity extends AppCompatActivity { //This is where it crashes, but this is default, I so stuck -------------------------------------------------------------------------------------------------------------------------------------- @Override //When created run the script below protected void onCreate(Bundle savedInstanceState) { //Supporting what is above super.onCreate(savedInstanceState); //Set the view to activity_main.xml setContentView(R.layout.activity_main);

//Grabbing the TextView washingtonFact final TextView washingtonFact = (TextView) findViewById(R.id.washingtonFact); //Grabbing the Button anotherFact Button anotherFact = (Button) findViewById(R.id.anotherFact); //Setting the OnClickListener View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { //Declaring the array facts String[] facts = { //The facts are written below "The Washington Monument, designed by Robert Mills and eventually completed by Thomas Casey and the U.S. Army Corps of Engineers.", "The Washington Monument was the tallest building in the world upon its completion in 1884.", "The structure was completed in two phases of construction, one private (1848-1854) and one public (1876-1884).", "The Washington Monument was built in the shape of an Egyptian obelisk.", "The monument is 555 feet tall", "The monument was supposed to be 600 feet tall with 100 feet tall columns, according to the Roberts Mills design.", "The monument's construction started in 1848.", "It was very hard to raise money for the monument.", "The monuments very tip, is made out of aluminum.", "There were stones from two-different quarries, that is why the first one-third of the monument is a different color.", "The pyramid or pyramidion on top of the tower is made out of 300-ton marble.", "On the east face of the aluminum is Laus Deo which is Praise be to God is inscribed their.", "At 1:51 p.m. on August 23, 2011, a magnitude 5.8 earthquake struck 90 miles southwest of Washington, shaking the monument.", "The original steam-driven elevator had a trip time of 10+ minutes to the top, was replaced with an electric elevator in 1901", "The monument had a steam-powered elevator that could lift six tons of stone up to a movable 20-foot-tall iron frame.", "It took $1,187,710 to build the Washington Monument.", "The Washington Monument had three (3) floors", "The plans for the Washington Monument began even before George Washington was elected president.", "The original design (by: Robert Mills) for the monument was way different than what ended up being built", "The monument was once the site of a terrorist attack by a 66-year old Navy veteran on December 8, 1982", "The exterior of the Washington Monument was completed on December 6, 1884.", "The Washington Monument consists of 36,491 blocks", "The Washington Monument weighs 90,854 tons.", "Inside the Washington Monument is an elevator and a 897-step stairway (WOW!).", "Lightning rods at the top protect the Washington Monument from lightning strikes.", "There is an observation deck at 500 feet from the ground.", "Work on the monument was stopped in 1854 (when the monument was only 152 feet tall), after donations dropped off.", "From Memorial Day to Labor Day, the hours are 9AM, From Labor Day to Memorial Day, the hours are 9AM to 5PM.", "The monument stands east to the reflecting pool and the Lincoln Memorial", "Robert Mills said that if they did not follow his plan, the monument would look like a stalk of asparagus.", "When the monument was built, it was the tallest building until the Eiffel Tower was built in 1889", "The monument was under restoration (maintenance) (they were fixing it) in 1999", "At the time, Aluminum was rare and expensive so the aluminum tip was special", "In 2001, their was a temporary visitor screening center to reduce terrorist attacks.", "The marble benches were placed to prevent motor veichles to enter, due to terrorist attacks and the Mayor incident.", "The best part of the monument is that it is free to visit and it is free to get tickets!", "The monument was opened to public on October 9, 1888", "The Washington Monument is not the only statue dedicated to George Washington"};

            String fact = "";

            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(facts.length);

            fact = facts[randomNumber];

            washingtonFact.setText(fact);

        }
    };
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Now by button doesnt wrk

You declared your clickListener and told it what to do, but you didn't "give" your button the listener.

anotherFact.setOnClickListener(listener);

Ok thank you

Ok thank you