ar.readLine not reading a input from user? - java

Summary
1. I am import java.io.console,i am not getting any error in my code,
code is correct, I thought, It's getting some error in eclipse
software... Error :
Exception in thread "main" java.lang.NullPointerException at
loops.Escapey.main(Escapey.java:9)
Coding:
package loops;
import java.io.Console;
public class Escapey {
public static void main(String[] args) {
Console ar = System.console(); // creating a new object for console
String name = ar.readLine("how old are you ?"); //reads a user input
System.out.printf("%s - pretty age",name);
}
}
Ouput :
- I except the output how old are you ?? 18 18 - pretty age
But the actual output is getting error... Exception in thread "main"
java.lang.NullPointerException at loops.Escapey.main(Escapey.java:9)

I assume you are running this into some IDE. Since System.console returns the attached console, if you run it into IDE, it will return NULL. The better approach is to use Scanner class. If you really want to use System.console, you will have to test it on some console. If on Mac, run the terminal. On Linux any of the terminal apps would work.

If we run this in IDE (Intellij), it is throwing null pointer exception. Since this is related to console, I tried to execute this in command line and it works fine.
Execute these steps in terminal or command line and it will work
1) javac Escapey.java
2) java Escapey

Related

Can not run oracle SQL embed in java by using intellij

Currently, I'm trying to embed some oracle SQL statements into java project and I'm using IntelliJ as compiler. The problem is, I have set ojdbc7.jar into Intellij,
but my computer still showing me errors like this:
Error: Cannot find or load the main class employee
Reason: java.lang.NoClassDefFoundError: Employee (wrong name: employee)
And what drives me crazy is, if I run commands:
javac -cp ojdbc7.jar employee.java
java -cp ojdbc7.jar;. employee
on cmd, the program will run successfully. But no matter how I try, employee.java cannot be compiled on intellij. And the cause of the error is as I mentioned above.
This is the beginning of my code:
import java.io.Console;
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
public class employee
{
public static void main(String[] args) throws SQLException, IOException
{
String user, pass, snum, namer;
int enumber;
Console console = System.console();
System.out.print("Enter your username: "); // Your Oracle ID with double quote
user = "my_oracle_Account"; //console.readLine();
System.out.print("Enter your password: "); // Password of Oracle Account
//console.readPassword();
pass = "my_password";
// Connection
DriverManager.registerDriver(new OracleDriver());
OracleConnection conn =
(OracleConnection)DriverManager.getConnection(
my_url,user,pass);
System.out.println("Program starts.");
// do something...
}
}
the codes have no problem, and also please don't focus on command java -cp ojdbc7.jar;. employee I know sometimes ";. " should be ":. ".
Previously, there are also other errors like concole is null
You can see that I have commented out console.readLine(). Because when I run it, the program won't stop and wait for input.
This seems relate to path problem but I cant solve it.
How to make intellij run the employee successfully? I need to debug the program, pls help me.
OK, problems solved. As Jens said after I change the file name from employee to Employee it works correctly. But why? Actually I download this codes from my professor's website so I never doubt its correctness! Such a stupid error...And why it could be run successfully on cmd? Just a nuance on "E" and "e". Last night I try to do this but because I have declared another class named Employee in the same Project file so I skipped this method. Such a stupid error and ... me!
Btw, thank you guys a lot.

Can't understand where is the problem (Java) [duplicate]

This question already has answers here:
Eclipse command line arguments
(4 answers)
Closed 3 years ago.
Trying to run a program that swaps names. The task is simple: input > Alice Bob Alex, output > Alex Bob Alice
P.s. Maybe the problem is stupid, but I just recently started programming, so I don't know what to do
I try to run the code in Eclipse - gives an index error. I start in the console - gives an error of the main name. Tried to pass through the internal debugger in Eclipse - writes that I am using obsolete methods. In the end, nothing is clear./
public class Noob {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print(args[2]);
System.out.print(" " + args[1]);
System.out.println(" " + args[0]);
}
}
Error message from Eclipse:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 0
at noob/noob.Noob.main(Noob.java:7)
Console (the file name is Noob.java)
First I compiled src file (> javac Noob.java)
Then I ran it (> java Noob)
Error: Could not find or load main class Noob
Caused by: java.lang.NoClassDefFoundError: noob/Noob (wrong name: Noob)
You need to pass three arguments
This is the steps you need to follow, to passing arguments
1-) Click on Run -> Run Configurations
2-) Click on Arguments tab
3-) In Program Arguments section , Enter your arguments.
4-) Click Apply
You are not passing correctly the values to your program. If you call the program from command line with java myProgram Alice Bob Alex your code should work.
Moreover, you can pass the values to your code through Eclipse directly inside Run Configuration option.
Also, it would be better to not hard code the values. Try this:
for (int i=args.length-1; i>=0; i--) {
System.out.print(args[i] + ' ');
}
System.out.println("");

Terminated nonerror in eclipse java regarding javaw.exe

I realize that when eclipse says:
<terminated> main [java application] C:\program files\java\jre1.8.0_25\bin\javaw.exe
That the program ran and terminated. My problem is that it doesn't really run, as soon as I hit run it immediately shows this message. The program doesn't run at all. Anyone know how to stop javaw.exe from terminating the program immediately?
public class main {
public static void main(final String[] args) throws Exception {
Twitchbot bot = new Twitchbot();
bot.setVerbose(true);
bot.connect("irc.twitch.tv", "6667", "oauth:*********************************");
bot.joinChannel("#donnie64");
}
}
That's because your program did run, and It did exit, all without error or output. It creates a Twitchbot, sets it to verbose, connects, joins the channel, then the program ends after that because it has no more instructions, loops, or threads to wait for. The only thing it says is
<terminated> main [java application] C:\program files\java\jre1.8.0_25\bin\javaw.exe
because that's what Eclipse is supposed to say when a program ends.
If there was an error, it should've printed something in the console in red letters like:
Exception in thread "main" java.lang.RuntimeException
at testing.NFATest.main(NFATest.java:45)
or whatever exception it threw. (NullPointerException, ArrayIndexOutOfBounds, etc)

Why does System.console() return null for a command line app? [duplicate]

This question already has answers here:
System.console() returns null
(13 answers)
Closed 8 years ago.
I am working on a legacy app which depends on user command line input:
String key = System.console().readLine("Please enter the license key: ");
However, I am getting a NullPointerException because System.console() gives me a null.
Why does System.console() return null for a command line app? It happens when running it out of the terminal as well as IDE.
If you start java from a terminal window, then it really should work, even though I haven't tried on OSX.
If you run a simple test using java directly from the terminal, does it work?
echo 'public class Test { public static void main(String[] args) {System.console().printf("hello world%n");}}' >Test.java && javac Test.java && java Test
Expected output:
hello world
If it doesn't work, then sorry, no console support on your platform.
However, if it works, and your program doesn't then there is a problem with how your program is started.
Check how the java binary started? Is it started from a shell script? Check that stdin/stdout have not been redirected or piped into something, and possibly also that it's not started in the background.
ex: This will probably make System.console() return null.
java Test | tee >app.log
and this:
java Test >/tmp/test.log
This seems to work on my machine (linux)
java Test &
Neither does it seem as if System.setOut, System.setErr or System.setIn affects the console, even after a couple of gc's and finalizers.
However:
Closing (the original) System.out or System.in will disable the console too.
echo 'public class Test { public static void main(String[] args) {System.out.close();System.console().printf("hello world%n");}}' >Test.java && javac Test.java && java Test
Expected output:
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:1)
So; scan your code for places where it closes streams, or passes System.out somewhere it might get closed.
To read from Standard input (command line input) you must use some kind of stream reader to read the System.in stream.
An InputStreamReader initialised by
InputStreamReader(System.in)
lets you read character by character.
However, I suggest wrapping this with a BufferedReader:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String inputLine = reader.readLine();
Must import
java.io.*;

Runtime error of Console()

I have installed the JDK version 1.6 now the code is compiled but throws a run-time error
class Console1
{
public static void main(String args[])
{
System.console().readLine("this is how we give he input to the string");
System.out.println("this is what we want to print:0)");
}
}
Output
exception in thread "main" java.lang.NoClassDefFoundError: Console
It looks like you are running with a different version of Java. Add this as the first line of code to your "application". It will show the java version which is used to run the application.
System.out.println(System.getProperty("java.version"));
I see this is related to your other thread here.
System.console() returns the Console object, which was added in JDK 1.6, you say you have installed jdk 1.6, so one can only assume that your versions of java you are using to compile and run your code are different.
Can you please tell me what you are using to compile the code, and how you are running the code?
In future, please use the same thread you initially posted, it's much easier for people to trace.
Instead of console you can use...
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String s = bufferRead.readLine();
see example

Categories