1

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!

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

"Use any size you need." Within OutOfMemoryError or MAX_VALUE. ;)
@Andrew Thompson: Agreed. Reasonable sizes only.
Also you may want to take a look at Java Collection API docs.oracle.com/javase/7/docs/api/java/util/Collections.html. It's much more powerful than arrays.
Thanks, this will work for now, ill take a look to te Collection later when i got some more time. Thanks for your awnsers!
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.