Running jar file using Scala returns java.lang.NullPointerException

Hi Durga
I am a beginner to Hadoop and Java. I was going through Udemy Hadoop tutorial and created a jar file using sbt as per your example. The name of the jar file which was created is user_2.10-0.1-SNAPSHOT.jar, the name is different in your example.In the build.sbt, the contents are(name := "hw"
version := "1.0"
scalaVersion := “2.12.2”). Then tried to run the jar file using scala as per your example but I am getting java.lang.NullPointerException. Went through lotz of internet forums and threads but not able to resolve the issue.
Please see below the exception. Please help me to figure out the issue.
Thanks
Bobby Sam


C:\Users\USER\target\scala-2.10>scala user_2.10-0.1-SNAPSHOT.jar World
java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at scala.reflect.internal.util.ScalaClassLoader.$anonfun$tryClass$1(Scal
aClassLoader.scala:44)
at scala.util.control.Exception$Catch.$anonfun$opt$1(Exception.scala:242
)
at scala.util.control.Exception$Catch.apply(Exception.scala:224)
at scala.util.control.Exception$Catch.opt(Exception.scala:242)
at scala.reflect.internal.util.ScalaClassLoader.tryClass(ScalaClassLoade
r.scala:44)
at scala.reflect.internal.util.ScalaClassLoader.tryToInitializeClass(Sca
laClassLoader.scala:40)
at scala.reflect.internal.util.ScalaClassLoader.tryToInitializeClass$(Sc
alaClassLoader.scala:40)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.tryToInit
ializeClass(ScalaClassLoader.scala:129)
at scala.reflect.internal.util.ScalaClassLoader.run(ScalaClassLoader.sca
la:91)
at scala.reflect.internal.util.ScalaClassLoader.run$(ScalaClassLoader.sc
ala:90)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(Scala
ClassLoader.scala:129)
at scala.tools.nsc.CommonRunner.run(ObjectRunner.scala:22)
at scala.tools.nsc.CommonRunner.run$(ObjectRunner.scala:21)
at scala.tools.nsc.JarRunner$.run(MainGenericRunner.scala:13)
at scala.tools.nsc.CommonRunner.runAndCatch(ObjectRunner.scala:29)
at scala.tools.nsc.CommonRunner.runAndCatch$(ObjectRunner.scala:28)
at scala.tools.nsc.JarRunner$.runAndCatch(MainGenericRunner.scala:13)
at scala.tools.nsc.JarRunner$.runJar(MainGenericRunner.scala:25)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala
:65)
at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:88)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:99)

    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:104)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

Mostly some dependent jar file is missing. Can you give more details on the application? if possible code on github?

I think this is due to Scala version mismatch. You project is build using 2.12 version and you are running in Scala 2.10. so build project using 2.10 and rerun it will run properly.

Hi Thanks for the response. Only one version of Scala is installed in the System. The Scala version installed is 2.12.2.
The contents of the “build.sbt” file is
name := "hw"
version := "1.0"
scalaVersion := “2.12.2”

The contents of the “hw.scala” file is
object hw {
def main(args: Array[String]) {
println(“Hello World!”)
}
}

When I build the package, I get “user_2.10-0.1-SNAPSHOT.jar” in "C:\Users\USER\target\scala-2.10 ".
Not sure why. I uninstalled Scala and installed it again. Again the same issue.

Thanks
Bobby Sam

I wrote a simple scala program.
hw.scala


object hw {
def main(args: Array[String]) {
println(“Hello World!”)
}
}


Then I build it into a jar.

build.sbt


name := "hw"
version := "1.0"
scalaVersion := “2.12.2”


When I build the package, I get “user_2.10-0.1-SNAPSHOT.jar” in "C:\Users\USER\target\scala-2.10 ".
When I execute this jar using “scala user_2.10-0.1-SNAPSHOT.jar”, I get the exception.

I had this java.lang.NullPointerException in another scenario when I executed a very simple MapReduce program using Eclipse. Not sure whether both these exceptions are related. Please see below the Code and the Exception.
Thanks in Advance
Bobby Sam

package com.nyse;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class RecordCount extends Configured implements Tool{

@Override
public int run(String[] arg0) throws Exception {
// TODO Auto-generated method stub
Job job = Job.getInstance(getConf());

FileInputFormat.setInputPaths(job, new Path("D:/deckofcards.txt"));
FileOutputFormat.setOutputPath(job, new Path("D:/output"));
return job.waitForCompletion(true) ? 0 : 1;

}
public static void main(String args[]) throws Exception{

System.exit(ToolRunner.run(new RecordCount(), args));
}
}


log4j:WARN No appenders could be found for logger (org.apache.hadoop.util.Shell).
log4j:WARN Please initialize the log4j system properly.

Exception in thread “main” java.lang.NullPointerException
at java.lang.ProcessBuilder.start(Unknown Source)
at org.apache.hadoop.util.Shell.runCommand(Shell.java:483)
at org.apache.hadoop.util.Shell.run(Shell.java:456)
at org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:722)
at org.apache.hadoop.util.Shell.execCommand(Shell.java:815)
at org.apache.hadoop.util.Shell.execCommand(Shell.java:798)
at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:728)
at org.apache.hadoop.fs.RawLocalFileSystem.mkOneDirWithMode(RawLocalFileSystem.java:486)
at org.apache.hadoop.fs.RawLocalFileSystem.mkdirsWithOptionalPermission(RawLocalFileSystem.java:527)
at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:504)
at org.apache.hadoop.fs.FilterFileSystem.mkdirs(FilterFileSystem.java:305)
at org.apache.hadoop.mapreduce.JobSubmissionFiles.getStagingDir(JobSubmissionFiles.java:133)
at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:144)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1290)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1287)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Unknown Source)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1657)
at org.apache.hadoop.mapreduce.Job.submit(Job.java:1287)
at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1308)
at com.nyse.RecordCount.run(RecordCount.java:18)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:84)
at com.nyse.RecordCount.main(RecordCount.java:22)

Try running your application using class path. Here is the sample command.

scala -classpath target/scaaaaaaaaala-1.0.jar scaaalaaa.App Hello World!

Just let me know what is the output…

Hi
I tried

C:\Users\USER>scala C:\Users\USER\target\scala-2.10\user_2.10-0.1-SNAPSHOT.jar W
orld

but again got the same Exception

Thanks