with the setting from my previous mail. Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.mycompany.add2strjop.Employee.getEmpName()" because* "arr[i]" is null* at com.mycompany.add2strjop.Add2StrJOp.showRecords(Add2StrJOp.java:84) at com.mycompany.add2strjop.Add2StrJOp.main(Add2StrJOp.java:104)
Met vriendelijke groet, Pieter van den Hombergh. Kerboschstraat 12 5913 WH Venlo On Sun, Jul 3, 2022 at 7:25 PM Pieter van den Hombergh < pieter.van.den.hombe...@gmail.com> wrote: > You declare an array 0f 10 elements, but fill only three. The 4th element > (at position 3) is still null, that is where you get you null pointer. > > Maybe it is a good time to: > 1. switch to java 17 which has better error messages for null pointers > when you start it with -XX:+ShowCodeDetailsInExceptionMessages. > 2. Add tests to your code so you are more selfsufficient. Maybe have a > glance at https://testfirstj.github.io/index.html that gives an > introduction to Test Driven Design. > > > Met vriendelijke groet, > Pieter van den Hombergh. > Kerboschstraat 12 > 5913 WH Venlo > > > On Sat, Jul 2, 2022 at 1:32 AM Zulfi Khan <zulfi6...@yahoo.com.invalid> > wrote: > >> Hi, >> >> I am getting a run-time error in the following program,using Netbeans ID: >> 14, ubuntu 18.04: >> >> >> /* >> >> * Click >> nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to >> change this license >> >> * Click >> nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java >> to edit this template >> >> */ >> >> >> package com.mycompany.add2strjop; >> >> import java.util.Arrays; >> >> import javax.swing.*; >> >> import java.lang.NumberFormatException; >> >> import java.io.FileWriter; >> >> import java.io.FileReader; >> >> import java.io.BufferedWriter; >> >> import java.io.BufferedReader; >> >> import java.io.IOException; >> >> import java.lang.Math; >> >> class Employee{ >> >> private String strEmpName; >> >> int iID; >> >> int iWage; >> >> Employee(){} >> >> int getID(){return iID;} >> >> int getWage(){return iWage;} >> >> String getEmpName(){return strEmpName;} >> >> void setID(int i){ iID = 1000 +i;} >> >> void setWage(int i){iWage = 10000 + 2*i;} >> >> void setEmpName(int i){strEmpName = "Emp"+i;} >> >> } >> >> public class Add2StrJOp { >> >> Employee[] obj = new Employee[10]; >> >> Employee[] empArrRegisterEmployee(){ >> >> for(int i=0; i<3; i++){ >> >> obj[i] = new Employee(); >> >> obj[i].setID(i); >> >> obj[i].setWage(i); >> >> obj[i].setEmpName(i); >> >> } >> >> return obj; >> >> } >> >> void showRecords(Employee[] arr){ >> >> for(int i=0; i< arr.length; ++i){ >> >> String strName = arr[i].getEmpName(); >> >> int iidTemp = arr[i].getID(); >> >> int iWageTemp = arr[i].getWage(); >> >> String res = "Name= "+strName+"id= "+Integer.toString(iidTemp)+"wage= >> "+Integer.toString(iWageTemp); >> >> JOptionPane.showMessageDialog(null, res); >> >> } >> >> } >> >> public static void main(String[] args){ >> >> Add2StrJOp obj = new Add2StrJOp(); >> >> Employee[] objEmp =obj.empArrRegisterEmployee(); >> >> obj.showRecords(objEmp); >> >> } >> >> } >> >> >> >> The error is: >> >> >> cd /home/zulfi/NetBeansProjects/add2StrJOp; >> JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 >> /snap/netbeans/64/netbeans/java/maven/bin/mvn -Dexec.vmArgs= >> "-Dexec.args=${exec.vmArgs} -classpath %classpath ${exec.mainClass} >> ${exec.appArgs}" -Dexec.appArgs= >> -Dexec.mainClass=com.mycompany.add2strjop.Add2StrJOp >> -Dexec.executable=/usr/lib/jvm/java-11-openjdk-amd64/bin/java >> org.codehaus.mojo:exec-maven-plugin:3.0.0:exec >> >> Running NetBeans Compile On Save execution. Phase execution is skipped >> and output directories of dependency projects (with Compile on Save turned >> on) will be used instead of their jar artifacts. >> >> Scanning for projects... >> >> >> ----------------< com.mycompany.add2strjop:add2StrJOp >----------------- >> >> Building add2StrJOp 1.0-SNAPSHOT >> >> --------------------------------[ jar ]--------------------------------- >> >> >> --- exec-maven-plugin:3.0.0:exec (default-cli) @ add2StrJOp --- >> >> Exception in thread "main" java.lang.NullPointerException >> >> at com.mycompany.add2strjop.Add2StrJOp.showRecords(Add2StrJOp.java:41) >> >> at com.mycompany.add2strjop.Add2StrJOp.main(Add2StrJOp.java:55) >> >> Command execution failed. >> >> org.apache.commons.exec.ExecuteException: Process exited with an error: 1 >> (Exit value: 1) >> >> at org.apache.commons.exec.DefaultExecutor.executeInternal >> (DefaultExecutor.java:404) >> >> at org.apache.commons.exec.DefaultExecutor.execute >> (DefaultExecutor.java:166) >> >> at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:982) >> >> at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:929) >> >> at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:457) >> >> at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo >> (DefaultBuildPluginManager.java:137) >> >> at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute >> (MojoExecutor.java:301) >> >> at org.apache.maven.lifecycle.internal.MojoExecutor.execute >> (MojoExecutor.java:211) >> >> at org.apache.maven.lifecycle.internal.MojoExecutor.execute >> (MojoExecutor.java:165) >> >> at org.apache.maven.lifecycle.internal.MojoExecutor.execute >> (MojoExecutor.java:157) >> >> at >> org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject >> (LifecycleModuleBuilder.java:121) >> >> at >> org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject >> (LifecycleModuleBuilder.java:81) >> >> at >> org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build >> (SingleThreadedBuilder.java:56) >> >> at org.apache.maven.lifecycle.internal.LifecycleStarter.execute >> (LifecycleStarter.java:127) >> >> at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294) >> >> at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) >> >> at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) >> >> at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960) >> >> at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293) >> >> at org.apache.maven.cli.MavenCli.main (MavenCli.java:196) >> >> at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) >> >> at jdk.internal.reflect.NativeMethodAccessorImpl.invoke >> (NativeMethodAccessorImpl.java:62) >> >> at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke >> (DelegatingMethodAccessorImpl.java:43) >> >> at java.lang.reflect.Method.invoke (Method.java:566) >> >> at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced >> (Launcher.java:282) >> >> at org.codehaus.plexus.classworlds.launcher.Launcher.launch >> (Launcher.java:225) >> >> at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode >> (Launcher.java:406) >> >> at org.codehaus.plexus.classworlds.launcher.Launcher.main >> (Launcher.java:347) >> >> ------------------------------------------------------------------------ >> >> BUILD FAILURE >> >> ------------------------------------------------------------------------ >> >> Total time: 5.002 s >> >> Finished at: 2022-07-01T18:09:59-05:00 >> >> ------------------------------------------------------------------------ >> >> Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec >> (default-cli) on project add2StrJOp: Command execution failed.: Process >> exited with an error: 1 (Exit value: 1) -> [Help 1] >> >> >> To see the full stack trace of the errors, re-run Maven with the -e >> switch. >> >> Re-run Maven using the -X switch to enable full debug logging. >> >> >> For more information about the errors and possible solutions, please read >> the following articles: >> >> [Help 1] >> http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException >> >> >> >> Somebody please guide me. >> >> >> Zulfi. >> >>