Comments (9)
It could be the way the project is setup in Gradle. My IntelliJ wasn't able to recognize the code to run. This project does not follow standard Gradle folder structure. The files were just put there as a reference for the book.
from java-fundamentals.
This is really helpful. A lot more details and it clarifies a lot.
Thank you for spending time on this.
from java-fundamentals.
This NullPointerException
happens if for some reason the place where you running the class from doesn't have a console
available. According to the Javadoc, it can happen but it is not clear where, when or how it would.
Where did you run this code from?
Using Java 8 Amazon's Correto JVM, I see no issues:
Java-Fundamentals/Lesson01/Exercise04 $ java -version
openjdk version "1.8.0_282"
OpenJDK Runtime Environment Corretto-8.282.08.1 (build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM Corretto-8.282.08.1 (build 25.282-b08, mixed mode)
Java-Fundamentals/Lesson01/Exercise04 $ javac ProductOfNos.java; java ProductOfNos
Enter the first number
10
Enter the Second number
10
The product of the two numbers is 100%
Also, it throws a NullPointerException
, adding the throws IOException
in there would not fix this issue. Sorry, I merged that too fast thinking it was a compilation issue.
from java-fundamentals.
I've tested this on three computers, one of which is a new computer that has never had a JDK installed. I downloaded Amazon Corretto Java 8. A fresh copy of IntelliJ for the new computer. Started new projects. Copied and inserted the code into the IntelliJ IDEAs And I'm receiving the same exceptions on all computers, even the new one:
"C:\Program Files\Amazon Corretto\jdk1.8.0_332\jre\bin\java.exe" ...
Enter the first number
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:7)
BUT, when I typed the code into Replit.com, it works. It uses Java 17.0.1. So I'm left baffled.
from java-fundamentals.
Did you type a number when it asked you to?
I just noticed that the NullPointerException
could also come from no input coming from the console and trying to parse that into a number.
Did the console waited for you to type something or it just explodes immediately?
from java-fundamentals.
Just exploded immediately. No input was ever entered.
from java-fundamentals.
I installed IntelliJ on Linux Mint (VM) with default open jdk 11.0.9.1 2020-11-04 and it threw the same exception. So I decided to get back to the basics by going to the terminal. Compiling the code with javac and running with java in a terminal worked. It may be an IntelliJ issue and I will bring the matter to their attention. Thank you for your help.
from java-fundamentals.
My IntelliJ defaults to its own build system called 'IntelliJ', but Maven and Gradle are offered as alternatives. I did more testing on various platforms, different IDE's (Eclipse, JGrasp..), different builds (Ant, Maven, Gradle) and all seem to throw the same exceptions. To be honest, this code is not something you see routinely in beginner Java textbooks as the Scanner object has taken precedence, although I like the syntax very much which reminds me of Pascal/Nim or C#.
var num1 = Int32.Parse(System.Console.ReadlLine()); //C#
But, after doing more research, it appears that System.Console()
may be the 'culprit'. From Oracle docs:
"Before a program can use the Console, it must attempt to retrieve the Console object by invoking
System.console()
. If the Console object is available, this method returns it. IfSystem.console()
returnsNULL
, then Console operations are not permitted, either because the OS doesn't support them or because the program was launched in a noninteractive environment."
From StackOverflow:
How do you launch your application? Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.
So perhaps this may be the reason the code runs from a terminal (or Replit) and not the IDE's. :)
from java-fundamentals.
Some additional info from IntelliJ's YouTrack issues.
from java-fundamentals.
Related Issues (2)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from java-fundamentals.