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

Java Java Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

shreya gupta
shreya gupta
486 Points

error in code

Exception in thread "main" java.lang.NullPointerException at javaapplication1.JavaApplication1.main(JavaApplication1.java:18) what is this error? my code was

package javaapplication1;

import java.io.Console; public class JavaApplication1 {

public static void main(String[] args) {

    Console console =System.console();
    // TODO code application logic here
    String ageInString ;
    ageInString= console.readLine("how old are you?  ");
    int age = Integer.parseInt(ageInString);
    if(age<18)
    {console.printf("not valid");
    System.exit(0);
    }

}

}

2 Answers

Loris D'Antonio
PLUS
Loris D'Antonio
Courses Plus Student 10,155 Points

Hello Shreya,

This issue probably because you are trying to run your code from IDE.

If you try to run the code from command line, probably will work.

Can you use Scanner object? if so, try:

    public static void main(String[] args) {

        // TODO code application logic here
        String ageInString;
        Scanner scanner = new Scanner(System.in);
        System.out.println("how old are you?  ");
        ageInString = scanner.nextLine();

        int age = Integer.parseInt(ageInString);
        if (age < 18) {
            System.out.printf("not valid");
            System.exit(0);
        }

    }
shreya gupta
shreya gupta
486 Points

it worked thankyou!