|
||
|
|||
|
Hello,
I'm trying to make a simple calculator with Java and everything compiles well but when I try to run it, I get an error on the line String expression = keyboard.nextLine(); saying "NullPointerException:null" I read a few webpages saying that it is trying to access something but that something is null? :S Thanks Java Code
import java.util.Scanner;
public class Calculator
{
private static Scanner keyboard;
public static void main(String[] args)
{
System.out.println("Welcome to calculator 1.0");
System.out.println();
System.out.print("Enter an expression: ");
String expression = keyboard.nextLine();
// The user must enter an expression such as 2+3.
// You can assume that only 1-digit numbers are typed so
// that you can use expression.charAt(0) to get the first
// number and so on.
int number1; // the first number
int number2; // the second number
char operation; // the operation (+,-,*,/)
number1 = expression.charAt(0);
operation = expression.charAt(1);
number2 = expression.charAt(2);
// Now calculate the answer and print it out.
//
// Use an if/else statement to handle each operation
int answer;
if (operation == '+')
answer = number1+number2;
else if (operation == '-')
answer = number1-number2;
else if (operation == '*')
answer = number1*number2;
else if (operation == '/')
answer = number1/number2;
else
{
System.out.println("Unsupported operation: " + operation);
return;
}
System.out.println("The answer is" +answer);
}
}
|
|
||||
|
Indeed, you have a null reference, when you attempt to call a method on a null reference you will be thrown a NullPointerException.
Java Code
String myString = null;
myString.length();
|
|
||||
|
sir json i notice his error and i corrected it.. but i need some brief explanation about the
correct instantiation that i've posted can you please explain it how? and why? tnx
|
|
||||
|
The dot operator references a method/field of an instantiated object. However, null means you're not pointing to an object, and can't reference a method/field of an object if it doesn't exist. So, the JVM is polite and throws a NullPointerException rather than crash your program/system
__________________
ASCII a question .. Get an ANSI Please surround your code with [highlight=Java]code goes here[/highlight]. |
|
||||
|
Here is a scary bit of code for you though.
Try running this. Java Code
public class MyClass {
public static void myMethod() {
System.out.println("No null pointer exception?");
}
public static void main(final String[] arguments) {
MyClass myclass = null;
myClass.myMethod();
}
}
|
|
||||
|
Eclipse is smart
It won't let me compile.You're trying to access a static method via an instantiated object. However, if you using a different compiler, it's likely you won't get a null pointer exception (if it's smart, and thinks it knows better than you do). The reason is it may go through on compile and replace all attempts to call static methods un-statically, and replace them so they are called statically. It's impossible to get a null pointer exception from calling a method statically
__________________
ASCII a question .. Get an ANSI Please surround your code with [highlight=Java]code goes here[/highlight]. |
|
||||
|
My eclipse compiles fine with that, you should be getting a compiler warning though.
You can call statics using "this" as well if you like, during runtime the JVM will just reference the class rather than the instance anyways. // Json |
|
||||
|
I think I may have turned a setting on so it will give me an error. That's ok, it makes no sense to try and use a static member non-statically
__________________
ASCII a question .. Get an ANSI Please surround your code with [highlight=Java]code goes here[/highlight]. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [SOLVED] NullPointerException.CLARIFICATION | chronoz13 | Exceptions | 8 | 28-08-2009 08:24 PM |
| Request.getParameter() value becoming null when i use entype="multipart/form-data" | radhikavk | JavaServer Pages: JSP & JSTL | 4 | 13-08-2009 10:54 AM |
| Null Pointer Exception runtime error | RoadRunner | Exceptions | 1 | 26-04-2009 06:21 PM |
| [SOLVED] crazy NullPointerException | dean_martin | Exceptions | 10 | 21-04-2009 11:40 AM |
| [SOLVED] NullPointerException | jazz2k8 | Exceptions | 9 | 01-06-2008 10:59 PM |
|
100 most searched terms
Search Cloud
|
| 2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? |