The source code is as follows:
Point[] points = new Point[n];//Point is a class for ( int i=0;i<n;i++ ) { System.out.print( "Please enter x:" ); points[i]. setX(in.nextInt());//The 29th line System.out.print( "Please enter y:" ); points[i].setY(in.nextInt()); }
The error message is as follows:
Exception in thread “main” java.lang.NullPointerException
at test.Main.zjd(Main.java:29)
at test.Main.main(Main.java:9)
analysis:
Point[] points=new Point[n];//After defining the class array, I thought that the above code could be used directly, but the above error was reported. So I set line 29 as a breakpoint, and when I started Debug and run to this point, I found the problem:
It is found in the variable list that the values of points[0] and points[1] are all null
It seems that there is no problem at a glance, but when you think about it, each element in the array is an object. Since it is an object, how can we use it without allocating space (without new)?
Modified code:
Point[] points= new Point[n]; for ( int i=0;i<n;i++ ) { points[i] =new Point(); //Solution: apply for allocating space for the elements in the class array here . System.out.print( "Please enter x:" ); points[i].setX(in.nextInt()); System.out.print( "Please enter y:" ); points[i].setY(in .nextInt()); }
Read More:
- [Solved] Exception in thread “main“ java.lang.NoSuchFieldError: level
- [Solved] qrcode-error: Exception in thread “main” java.lang.NoClassDefFoundError…
- [Solved] Docker Elasticsearch8.4.0 Error: Exception in thread “main” java.nio.file.FileSystemException
- [Solved] Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- [Solved] Hbase-shell 2.x Error: Unhandled Java exception: java.lang.IncompatibleClassChangeError: Found class jline.Terminal…
- [Solved] java: Internal error in the mapping processor: java.lang.NullPointerException
- Request processing failed; nested exception is java.lang.NullPointerException or UnsatisfiedDependencyE
- [Solved] Hadoop Error: Exception in thread “main“ java.io.IOException: Error opening job jar: /usr/local/hadoop-2.
- Java error: unable to find or load main class (package name in source file)
- java: java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x590
- [Solved] Jenkins Publish to rancher Error: ERROR: Build step failed with exception java.lang.ClassCastException: class com.
- Java Running Error: Could not find or load main class
- Tinyint error: java.lang.NullPointerException… [How to Solve]
- [Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)
- [Solved] nacos Startup Error: nested exception is java.lang.RuntimeException: java.lang.RuntimeException: [db-load-error
- [Solved] Java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.util.List
- [Solved] Openfegn Remote Call Error: java.lang.NullPointerException: null
- [Solved] Initialization of anonymous inner class member variable causes java.lang.stackoverflowerror