spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Exception in thread "main" java.lang.NullPointerException (Java, Netbeans)


!alunos[numAl][0].equalsIgnoreCase("FIM")) will never evaluate how you want. When the condition is evaluated, alunos[numAl][0] is always checking against a NULL entry, because you've incremented numAl. The reason the YODA condition did something different for you is because "FIM" doesn't equal NULL, which means !"FIM".equalsIgnoreCase(alunos[numAl][0]); will always evaluate to TRUE, so the loop continues based solely on the .length() condition.

Try this:

public static int
lerInformação(String[][] alunos, int numAl) {
do {
    System.out.print("Nome do Aluno: ");
    alunos[numAl][0] = ler.next();
    System.out.print("Número do Aluno: ");
    ler.nextLine();
    alunos[numAl][1] = ler.next();
    numAl++;
} while(numAl < alunos.length &&
(!alunos[numAl-1][0].equalsIgnoreCase("FIM")));
//numAl - 1, instead.
return numAl;

}

By using numAl - 1 you're checking against the most recent user input, which happened before you incremented numAl. However, a better solution would be:

public static int
lerInformação(String[][] alunos, int numAl) {
do {
    System.out.print("Nome do Aluno: ");
    alunos[numAl][0] = ler.next();
    if(alunos[numAl][0].equalsIgnoreCase("FIM") {
      break;
    }
    System.out.print("Número do Aluno: ");
    ler.nextLine();
    alunos[numAl][1] = ler.next();
    numAl++;
} while(numAl < alunos.length);
return numAl;
}

This way, you don't keep running unnecessary code(such as prompting for Numero do Aluno), once the user has entered FIM. The break will exit the loop immediately.

However, you'll still have to address the fact that you're storing a command (FIM), inside the user-generated array of alunos, which is bad practice. You can solve that for updating the relevant code to something like:

 String userInput = ler.next();
    if(userInput.equalsIgnoreCase("FIM") {
      break;
    }
    else {
     alunos[numAl][0] = userInput;
    }

Categories : Java

Related to : Exception in thread "main" java.lang.NullPointerException (Java, Netbeans)
HW with exception (NullPointerException)
You never initialze the 2nd dimineion of your code array, and so it remains null, since all arrays must be initialized before you start to fill them. So do that: char[][] code = new char[w][]; for (int i = 0; i < w; i++) { // **** here **** code[i][] = new char[words[i].length()]; // ***** for (int j = 0; j < words[i].length(); j++) { code[i][j] = words[i].charAt(j);

Categories : Java
Netbeans exception class not found for javax.swing.JOptionPane
That is because of the OSGi framework being used. OSGi is a standard for modular systems where a module ("bundle" consisting of some (versioned) jars) maybe loaded in isolation with dependencies to other modules. It has a life-time management and separation of modules - realized with specific class loaders. This is a much more strict modular system that what java offers, with visibility and versi

Categories : Java
I’m using Deserialization method to create an object getting Exception “NullPointerException”?
You need to pass the serialized file as the input to the InputStream and pass its object to ObjectInputStream. TestEncaps object4 = null; FileInputStream inputFileStream = new FileInputStream("file.dat"); ObjectInputStream objectInputStream = new ObjectInputStream(inputFileStream); object4 = (TestEncaps)objectInputStream.readObject();

Categories : Java
WPF DependencyObject calling thread exception
You can wrap your call inside an Action() being called from Dispatcher like this: this.Dispatcher.BeginInvoke(new Action(() => { // your code accessing UI elements here }));

Categories : C#
"Exception in thread "main" java.util.InputMismatchException"**
Nothing is wrong with your code. Respect the type when you are entering your data. Do not enter a double while you are expecting an integer, etc. You can get around this type of error by applying defensive coding where you only accept data from the user when it complies with the expected value. public static void main(String[] arg) { Scanner keyboard = new Scanner(System.in); System.

Categories : Java
Recently Add
Concurrent collection to 50/50 read/write
How to get unique members of object class (groupofUniqueNames) from an ldap server using java
Custom error reporting: how to log down function arguments (and possibly locals) in an exception handler?
Websphere MQ message redelivery
How to use BigDecimal to achieve following code instead of using double or float?
What exactly do the service() method of this HttpServlet?
Why does the Java API use int instead of short or byte?
Hibernate. One to one mapping without cross references
How to pass array to a java method
BooleanFieldEditor doesn't load value from PreferenceStore
NoClassDefFoundError when I call the class through commnad line
Shopizer Search
Java - Bank System + Transaction Function
Hadoop security using Apache DS / Apache Directory Studio
Multiple property-placeholder with overlapping locations does not work?
could not remove/add jpanel in jframe
Java garbage collection verbose not working
If child class has a variable with same name as private variable in parent what will be used?
Jackson parse json with a one-to-many relationship in a single step
Excel opens wrong view after generating in Apache POI
On Switch How to use logic operator on case JAVA
Java: What is the absolut first second in Calendar?
Waiting One Second for a button click, if button not clicked call function
Fastest way to extract part of a large JSON File
copy directory operation using linux command in java
Figuring out how to Format Printing
Why does @FunctionalInterface have a RUNTIME retention?
Collision detection Libgdx rectangles
Cannot find symbol in method changeRecL but declared in main method
Receiving "java.net.SocketException: Connection reset" when connecting from dotnet client to a java server
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.