Seem like you required dynamic list, so what you need is replace String[] data = null; to use List

List data = new ArrayList<String>();
String[] data2 = null;
String[] datas = res.split("(s1)");
         
int i1 = 0;
int i2 = 0;
     
for (String datasx : datas) {
    i1++;
    String[] datas2 = datasx.split("(s2)");

    for (String datas2x : datas2) {
        String[] odcinek = datas2x.split("(s3)");
        data.add(odcinek[1] + "////" + odcinek[2] + "////" + odcinek[6]);
        i2++;
    }
}
Answer from Pau Kiat Wee on Stack Overflow
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ handle-null-pointer-access-eclipse
How to Handle Potential Null Pointer Access in Eclipse: A Simple Example - CodingTechRoom
Null pointer access is a common issue in Java programming that can lead to runtime exceptions. This guide outlines how to identify, handle, and prevent null pointer accesses in your Eclipse IDE environment.
Discussions

eclipse - 'Null pointer access' when using java output parameters - Stack Overflow
Java is pass by value. When you assign a value to obj in myMethod, you assign a new value to a copy of the pointer of the calling code. The calling code's pointer continues pointing to its initial object, which is null. This is why Eclipse warns you : you're trying to use objA, which is null, ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Potential null pointer access - Stack Overflow
I encounter a strange situation that is currently not that clear to me: When having the potential null pointer access warning enabled in Eclipse, I get warnings like in the following (the warnings... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 22, 2013
Improper warning - Potential null pointer access
There was an error while loading. Please reload this page ยท The original issue - redhat-developer/vscode-java#3124 More on github.com
๐ŸŒ github.com
0
October 2, 2023
exception - How to suppress null pointer access warning in java? - Stack Overflow
I am using eclipse IDE. I have a code which throws NullPointerException and I am handling the exception using try/catch block This is my code - String name = null; int length = 0... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 7
5

Since there is no concept of out parameters in java ...

The reference obj is a copy of the reference objA, so assigning a new object to obj will not change objA. This means that using obj as an out parameter is not supported by java.

Because of this a java method can return only one value. The following are possible solutions/workarounds.

Solution 1: exception instead of boolean (only if boolean indicates an error)

ClassA objA = null;  
try{
   objA = myMethod();
   //DO something with objA
}catch(MyException ex){
}

Solution 2: return null in case of an error.

ClassA objA = null;  

   objA = myMethod();
  if(objA != null)
{ //DO something with objA
}

Solution 3: use a Pair to return several values

MyPair mp = myMethod();
if(mp.first){
}

MyPair myMethod(){
   MyPair ret = new MyPair();
   mp.first = ...;//boolean
   mp.second = new ClassA();
   return ret;
}

class MyPair {
   boolean first;
   ClassA second;
}

Soultion 4: use single element arrays - ugly only use in extreme cases

ClassA[] objA = new ClassA[1];
if(myMethod(objA))
{
}

boolean myMethod(ClassA[] obj){
   obj[0] = new ClassA();
}
2 of 7
3

You can't really do what you're trying to do in java since, as you say, java doesn't support output parameters.

Method parameters are local variables, so making assignments to them has no effect outside the scope of the method. In other words, this:

public void foo(Object obj) {
    obj = new Object();
}

Is effectively equivalent (from the point of view of the code calling foo(Object)) as this:

public void foo() {
    Object obj = new Object();
}

Pointless, since the created object will be thrown out once foo() returns.

Probably, what you want to do is change your method to return the object your method creates:

public ClassA myMethod() {
   ClassA obj = ....
   ...
   return obj;
}

Then in your calling code:

ClassA objA = myMethod();
if (objA != null) {
    ...
}

Alternatively, you could instantiate the instance of ClassA outside your method and pass that value is, and have the method modify it in some way:

public boolean myMethod(ClassA obj) {
    obj.setValue(...);
    return true;
}

...

ClassA objA = new ClassA();
if (myMethod(objA) {
   Object val = objA.getValue();
   ...
}

Without knowing more about your specific problem, it's hard to say which design is better.

UPDATE:

The multi-parameter example you added is flat-out impossible in java, unfortunately. Everyone says java is pass-by-reference, but in reality it's more like pass-by-reference-value. A method can modify objects passed to it, but it cannot modify what the variables in the calling scope refer to. If you come from a C++ background, object references in java are more like pointers (without the pointer arithmetic) than they are like C++ references.

To make this more concrete, consider the following class:

public class ParameterPassing {
    public static void setParams(Integer value1, Integer value2) {
        System.out.println("value1 before: " + value1);
        System.out.println("value2 before: " + value2);
        value1 = new Integer(1);
        value2 = new Integer(2);
        System.out.println("value1 after: " + value1);
        System.out.println("value2 after: " + value2);
    }

    public static void main(String[] args) {
        Integer valNull = null;
        Integer val0 = new Integer(0);
        System.out.println("valNull before: " + valNull);
        System.out.println("val0 before: " + val0);

        setParams(valNull, val0);

        System.out.println("valNull after: " + valNull);
        System.out.println("val0 after: " + val0);
    }
}

When you run this, you'll get this output:

valNull before: null
val0 before: 0
value1 before: null
value2 before: 0
value1 after: 1
value2 after: 2
valNull after: null
val0 after: 0

As you can see, the assignments inside the setParams() method have no effect on what valNull and val0 refer to.

If you really need multiple "output" parameters for a single method, you're going to have to wrap them in some other object, or rethink your design. Perhaps you could make the references member variables rather than locals and have your method modify them directly:

public class MyClass {
    private ClassA objA;
    private ClassB objB;

    ...

    private boolean initObjects() {
        objA = ...;
        objB = ...;
        return true;
    }

    public void otherMethod() {
        ...
        if(initObjects() {
            // Use objA, objB
        }
    }
}
๐ŸŒ
The Eclipse Foundation
eclipse.org โ€บ forums โ€บ index.php โ€บ t โ€บ 174752
Eclipse Community Forums: Java Development Tools (JDT) ยป erroneous "Potential null pointer access" warning | The Eclipse Foundation
January 9, 2010 - The Eclipse Foundation - home to a global community, the Eclipse IDE, Jakarta EE and over 350 open source projects, including runtimes, tools and frameworks.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 16687820 โ€บ potential-null-pointer-access
java - Potential null pointer access - Stack Overflow
May 22, 2013 - From what I understand, Eclipse raises the warning once you assume a variable to potentially be null in the preceding code.
๐ŸŒ
GitHub
github.com โ€บ eclipse-jdt โ€บ eclipse.jdt.core โ€บ issues โ€บ 1461
Improper warning - Potential null pointer access ยท Issue #1461 ยท eclipse-jdt/eclipse.jdt.core
October 2, 2023 - import java.util.Objects; public class Test { public void test() { String name = null; if (Objects.isNull(name)) { System.out.println("Name is null"); return; } System.out.println(name.substring(0, 4));// Warning } } Null pointer access: The variable name can only be null at this location
Author: eclipse-jdt
๐ŸŒ
Eclipse
bugs.eclipse.org โ€บ bugs โ€บ show_bug.cgi
451660 โ€“ [compiler][null] Wrong "Null pointer access: The variable can only be null at this location" warning
Bugzilla โ€“ Bug 451660 [compiler][null] Wrong "Null pointer access: The variable can only be null at this location" warning Last modified: 2015-01-28 00:42:20 EST
Find elsewhere
๐ŸŒ
Eclipse
bugs.eclipse.org โ€บ bugs โ€บ show_bug.cgi
433615 โ€“ [compiler][null] "Potential Null Pointer Access" shows as error, instead of warning as specified in the Preferences
Bugzilla โ€“ Bug 433615 [compiler][null] "Potential Null Pointer Access" shows as error, instead of warning as specified in the Preferences Last modified: 2018-03-08 23:16:04 EST
๐ŸŒ
Eclipse
bugs.eclipse.org โ€บ bugs โ€บ show_bug.cgi
195638 โ€“ [compiler][null][refactoring] Wrong error : "Null pointer access: The variable xxx can only be null at this location " with try..catch in loop
Bugzilla โ€“ Bug 195638 [compiler][null][refactoring] Wrong error : "Null pointer access: The variable xxx can only be null at this location " with try..catch in loop Last modified: 2014-12-09 10:29:58 EST
๐ŸŒ
Eclipse
bugs.eclipse.org โ€บ bugs โ€บ show_bug.cgi
331222 โ€“ Unnecessary warning Potential null pointer access on auto-unboxing in loop
Bugzilla โ€“ Bug 331222 Unnecessary warning Potential null pointer access on auto-unboxing in loop Last modified: 2019-10-05 02:43:19 EDT
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 437843 โ€บ java โ€บ Null-pointer-access-variable-null
Null pointer access. The variable can be null (Beginning Java forum at Coderanch)
March 26, 2009 - Lets play it step by step: wireSize is not null => "wireSize!= null" resolves to true => whole condition resolves to true => if branch is executed. wireSize is null => "wireSize!= null" resolves to false => next condition is evaluated => NullPointerException I guess you wanted to use and (&&): ... So where is the error then ? Hi Sagar, thanks for your reply. There's actually no error. It's just my eclipse displaying a warning sign and I wanted to know what is meant by the message.
Top answer
1 of 12
4227

There are two overarching types of variables in Java:

  1. Primitives: variables that contain data. If you want to manipulate the data in a primitive variable you can manipulate that variable directly. By convention primitive types start with a lowercase letter. For example variables of type int or char are primitives.

  2. References: variables that contain the memory address of an Object i.e. variables that refer to an Object. If you want to manipulate the Object that a reference variable refers to you must dereference it. Dereferencing usually entails using . to access a method or field, or using [ to index an array. By convention reference types are usually denoted with a type that starts in uppercase. For example variables of type Object are references.

Consider the following code where you declare a variable of primitive type int and don't initialize it:

int x;
int y = x + x;

These two lines will crash the program because no value is specified for x and we are trying to use x's value to specify y. All primitives have to be initialized to a usable value before they are manipulated.

Now here is where things get interesting. Reference variables can be set to null which means "I am referencing nothing". You can get a null value in a reference variable if you explicitly set it that way, or a reference variable is uninitialized and the compiler does not catch it (Java will automatically set the variable to null).

If a reference variable is set to null either explicitly by you or through Java automatically, and you attempt to dereference it you get a NullPointerException.

The NullPointerException (NPE) typically occurs when you declare a variable but did not create an object and assign it to the variable before trying to use the contents of the variable. So you have a reference to something that does not actually exist.

Take the following code:

Integer num;
num = new Integer(10);

The first line declares a variable named num, but it does not actually contain a reference value yet. Since you have not yet said what to point to, Java sets it to null.

In the second line, the new keyword is used to instantiate (or create) an object of type Integer, and the reference variable num is assigned to that Integer object.

If you attempt to dereference num before creating the object you get a NullPointerException. In the most trivial cases, the compiler will catch the problem and let you know that "num may not have been initialized," but sometimes you may write code that does not directly create the object.

For instance, you may have a method as follows:

public void doSomething(SomeObject obj) {
   // Do something to obj, assumes obj is not null
   obj.myMethod();
}

In which case, you are not creating the object obj, but rather assuming that it was created before the doSomething() method was called. Note, it is possible to call the method like this:

doSomething(null);

In which case, obj is null, and the statement obj.myMethod() will throw a NullPointerException.

If the method is intended to do something to the passed-in object as the above method does, it is appropriate to throw the NullPointerException because it's a programmer error and the programmer will need that information for debugging purposes.

In addition to NullPointerExceptions thrown as a result of the method's logic, you can also check the method arguments for null values and throw NPEs explicitly by adding something like the following near the beginning of a method:

// Throws an NPE with a custom error message if obj is null
Objects.requireNonNull(obj, "obj must not be null");

Note that it's helpful to say in your error message clearly which object cannot be null. The advantage of validating this is that 1) you can return your own clearer error messages and 2) for the rest of the method you know that unless obj is reassigned, it is not null and can be dereferenced safely.

Alternatively, there may be cases where the purpose of the method is not solely to operate on the passed in object, and therefore a null parameter may be acceptable. In this case, you would need to check for a null parameter and behave differently. You should also explain this in the documentation. For example, doSomething() could be written as:

/**
  * @param obj An optional foo for ____. May be null, in which case
  *  the result will be ____.
  */
public void doSomething(SomeObject obj) {
    if(obj == null) {
       // Do something
    } else {
       // Do something else
    }
}

Finally, How to pinpoint the exception & cause using Stack Trace

What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?

Sonar with find bugs can detect NPE. Can sonar catch null pointer exceptions caused by JVM Dynamically

Now Java 14 has added a new language feature to show the root cause of NullPointerException. This language feature has been part of SAP commercial JVM since 2006.

In Java 14, the following is a sample NullPointerException Exception message:

in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "list" is null

List of situations that cause a NullPointerException to occur

Here are all the situations in which a NullPointerException occurs, that are directly* mentioned by the Java Language Specification:

  • Accessing (i.e. getting or setting) an instance field of a null reference. (static fields don't count!)
  • Calling an instance method of a null reference. (static methods don't count!)
  • throw null;
  • Accessing elements of a null array.
  • Synchronising on null - synchronized (someNullReference) { ... }
  • Any integer/floating point operator can throw a NullPointerException if one of its operands is a boxed null reference
  • An unboxing conversion throws a NullPointerException if the boxed value is null.
  • Calling super on a null reference throws a NullPointerException. If you are confused, this is talking about qualified superclass constructor invocations:
class Outer {
    class Inner {}
}
class ChildOfInner extends Outer.Inner {
    ChildOfInner(Outer o) { 
        o.super(); // if o is null, NPE gets thrown
    }
}
  • Using a for (element : iterable) loop to loop through a null collection/array.

  • switch (foo) { ... } (whether its an expression or statement) can throw a NullPointerException when foo is null.

  • foo.new SomeInnerClass() throws a NullPointerException when foo is null.

  • Method references of the form name1::name2 or primaryExpression::name throws a NullPointerException when evaluated when name1 or primaryExpression evaluates to null.

    a note from the JLS here says that, someInstance.someStaticMethod() doesn't throw an NPE, because someStaticMethod is static, but someInstance::someStaticMethod still throw an NPE!

* Note that the JLS probably also says a lot about NPEs indirectly.

2 of 12
972

NullPointerExceptions are exceptions that occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger a NullPointerException. These are the most common, but other ways are listed on the NullPointerException javadoc page.

Probably the quickest example code I could come up with to illustrate a NullPointerException would be:

public class Example {

    public static void main(String[] args) {
        Object obj = null;
        obj.hashCode();
    }

}

On the first line inside main, I'm explicitly setting the Object reference obj equal to null. This means I have a reference, but it isn't pointing to any object. After that, I try to treat the reference as though it points to an object by calling a method on it. This results in a NullPointerException because there is no code to execute in the location that the reference is pointing.

(This is a technicality, but I think it bears mentioning: A reference that points to null isn't the same as a C pointer that points to an invalid memory location. A null pointer is literally not pointing anywhere, which is subtly different than pointing to a location that happens to be invalid.)

๐ŸŒ
Dronatechnoworld
dronatechnoworld.com โ€บ 2023 โ€บ 01 โ€บ what-is-null-pointer-error-in-java-and.html
Technology World: What is Null Pointer Error in Java and How to fix it?
January 24, 2023 - In summary, a null pointer error occurs when a program tries to access an object or variable that has a null value. The error can be fixed by checking for null values before trying to access an object and handling the error properly.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-lang-nullpointerexception
Java NullPointerException - Detect, Fix, and Best Practices | DigitalOcean
August 3, 2022 - Technical tutorials, Q&A, events โ€” This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.