-3

Exception in thread "main" java.lang.NullPointerException at lab2.Test.main(Test.java:23)

package lab2;

import java.io.Console;

public class Test {

    public static void main(String []args) {

        Console cnsl = null;
        String  payload = null;



        // creates a console object
        cnsl = System.console();

        // read line from the user input
        its a run time error , it shows the null exception 

        payload = cnsl.readLine("Enter weight of the payload in lb: ");

        // prints

        pay = Float.parseFloat(payload);;

read line from the user input double hello = ((8 * pay * POUNDINKILO * ACCELERATION_GRAVITY)/(PI * ROWDENSITY * 0.75 * VOLUME * VOLUME));

        D = Math.sqrt(hello);

        System.out.println("For a payload of : " + payload);
        System.out.println("\nFor a payload of : " + payload);

        System.out.println("Radius is : " + D);
    }
}
chinni
  • 1
  • 1
  • 3
    `System#console` returns null for a few IDE´s, you should rather work with a `Scanner` and `System.in` – SomeJavaGuy Sep 09 '15 at 06:19
  • As this is very specific issue of System.console(), title of this questions should be change to "System.console() returning NULL" – codiacTushki Sep 09 '15 at 06:38

3 Answers3

1
payload = cnsl.readLine("Enter weight of the payload in lb: ");

It will depend on your environment.

You can try this.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         System.out.print("Enter weight of the payload in lb: ");
         String s = br.readLine();
SaviNuclear
  • 882
  • 6
  • 19
0

Instead of

cnsl = System.console();
payload = cnsl.readLine("Enter weight of the payload in lb: ");

Use java.util.Scanner like

Scanner in = new Scanner(System.in);
System.out.println(("Enter weight of the payload in lb: "));
payload = in.nextLine();

Here is your complete program using Scanner

import java.util.Scanner;
public class Test{

public static void main(String[] args) {
    String payload = null;
    final double ACCELERATION_GRAVITY = 9.81;
    final double PI = 3.14;
    final double ROWDENSITY = 1.22;
    final int VOLUME = 3;
    final double POUNDINKILO = 0.453592;
    double D;
    double pay;

    Scanner in = new Scanner(System.in);

    System.out.println(("Enter weight of the payload in lb: "));
    payload = in.nextLine();

    pay = Float.parseFloat(payload);

    double hello = ((8 * pay * POUNDINKILO * ACCELERATION_GRAVITY) / (PI
            * ROWDENSITY * 0.75 * VOLUME * VOLUME));

    D = Math.sqrt(hello);

    System.out.println("For a payload of : " + payload);
    System.out.println("\nFor a payload of : " + payload);
    System.out.println("Radius is : " + D);
    }
}
codiacTushki
  • 650
  • 1
  • 8
  • 20
0

The Java docs say,

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.

This is usually the case when you run your code on an IDE.


What do I do now ? Have a look at the Scanner class in java.util. More on this here

Community
  • 1
  • 1
Ghazanfar
  • 1,353
  • 12
  • 21