简体   繁体   中英

Java.lang.nullpointer exception in a sourcecode

Okay, here is the sourcecode -

The program should be able to run a) if no arguments are given, ask for a file name with args b) if there are args, get these args as parameters for method 'parametr'

The problem is, program works fine with the file input, but if the args are given from CMD or Eclipse. From my point of view, the code is totally fine, but IDK...

//block of a code, thats creates a output file + that should hand //over a Integer to method 'parametr' from the args array

else if (args.length > 0) {

                try {

                    PrintStream ps = new PrintStream("vystup.txt");

                    for (int i = 0; i < args.length; i++) {

                        parametr(Integer.parseInt(args[i]));
                    }
                    ps.close();
                }
                catch (Exception g) {

                    g.printStackTrace();
                    System.exit(1);
                }

            }
        }

this points at a Method 'parametr' >>

// this method should just create an array named 'pseudoposloupnost'via method //'Posloupnost' and then copy this array into a new array named 'serazenaPosloupnost' // rest of the code is not important

public static void parametr (int n) { 

                Posloupnost(n); //Another method to count array 'pseudo...'
                serazenaPosloupnost = new int [pseudoposloupnost.length];
                for (int k = 0; k < pseudoposloupnost.length; k++) {
                    serazenaPosloupnost[k] = pseudoposloupnost[k];
                }

                serazeniPosloupnosti(serazenaPosloupnost);

        ps.println(pseudoposloupnost.length + " " + Arrays.toString(pseudoposloupnost));
        ps.println(serazenaPosloupnost.length + " " + Arrays.toString(serazenaPosloupnost));
        ps.println();
        drawPosloupnost();

    }

Java points at these two blocks as a nullpointer exception when I try to run the code from CMD with arguments given.

I think you have two variables called ps . One of them is local to your try block, and the other is (probably) a static class variable. "Probably" because you don't show us its declaration.

Simplified down:

public class myClass {
    private static PrintStream ps; // we'll refer to this as "ps1"

    public static void parametr(int n) {
         ...
         ps.println("foo");
    }

    public static void myMethod() {
         try {
            PrintStream ps = 
               new PrintStream("x.txt"); // we'll refer to this as "ps2"
            parametr(1);
            ps.close();
         } catch (...) {
         }
    }
}

This is a matter of scope .

ps1 and ps2 are two different variables.

ps1 is defined but never initialised, so it has a value of null throughout the program.

ps2 is local to the try block surrounding it. It is not passed to parametr(), therefore parametr() doesn't see it.

When parametr() executes ps.println() , it looks at ps1, which is null, hence the NullPointerException.

One way you could fix this is to not create ps1, and pass ps2 into parametr():

public class myClass {

    public static void parametr(int n, PrintStream printStream) {
         ...
         printStream.println("foo");
    }

    public static void myMethod() {
         try {
            PrintStream ps = 
               new PrintStream("x.txt"); // we'll refer to this as "ps2"
            parametr(1, ps);
            ps.close();
         } catch (...) {
         }
    }
}

It's generally good to do this kind of thing, because you can see exactly what variables your methods need and can touch.

Another way you could fix it is by removing the PrintStream from the ps = ... statement in the try block, so you assign to the class variable. This is often not such a good idea, because you're hiding the management of that PrintStream from the reader of the code.


One other tip: you call a method posloupnost(n) (I changed its first letter to lowercase, because Java programmers prefer that). I can guess that either:

  • this method is a waste of time (because its only parameter is an integer, so the method can't change it)
  • this method has side effects on a class variable or a global variable.

It's almost always better to pass in the objects that will be affected by the method, so that it's clear what effects it will have. Even if you're going to be printing to the screen, it's better to do:

System.out.println(posloupnost(n));

... or ...

posloupnost(n, System.out);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM