Jab
Jab

Reputation: 929

2 dimensional string array declaration, Exception in thread "main" java.lang.NullPointerException java

I'm new to java programming and I can't find a solution to my problem. I think it's a pretty easy problem but I can't figure a what I'm doing wrong so I hope one of you could help me. The problem is when I try to store some data in an array it returns the following error:

Exception in thread "main" java.lang.NullPointerException

Code

public class FetchData{
    private String rows[][];
    private int rowCount;

    public FetchData(){
        rowCount = 0;
    }

    public boolean ConnectAndFetch(String start, String end){

        //not relevant to the problem
        for(...){
            List<WebElementdivList = driver.findElements(By.tagName("div"));

            int divCount = 0;
            int colCount = 0;
            for (WebElement elem : divList) {
                if(divCount 24){ 
                    if(colCount < 17){
                        System.out.println(elem.getText());
                        //System.out.println(colCount);
                        //System.out.println(rowCount);
                        rows[rowCount][colCount] = elem.getText();
                        colCount++;
                    } else { 
                        rowCount += 1;
                        colCount = 0;
                    }
                }
                divCount++;
            }
        }
        return true;
    }

I think it has something to do with the declaration private String rows[][]; but I don't know how to fix it. I'd appreciate your help!

Upvotes: 1

Views: 20310

Answers (2)

Vipul
Vipul

Reputation: 836

You declared string array but not initialized it... before using it you must initialized it as
row=new String [1][1]. Since you declared array as a instance variable the default value assign to it is null that's why u are getting null pointer exception. For more about array visit following link:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Upvotes: 4

Bernard
Bernard

Reputation: 7961

You need to initialize your rows array first before you can store values in it:

String[][] rows = new String[10][10];

Use any size you need. I chose 10 randomly. I suggest initializing the array in your class constructor.

Upvotes: 9

Related Questions