0

i am building a server to sends questions to clients and then receive answer from them, but when i send a question to a client i want to make it as unaviable for sending again for one hour, i mean after i send a question to client , i want for one hour that question will never sends to any other clients,

static Hashtable<Integer, List<Integer>> unavialbeQuestions =
new Hashtable<Integer, List<Integer>>();

the key is the clientID, the value is list of the IDs of questions that will never send to that client in one hour, this function i used to make question as unaivalble

public void setUnavialbeQuestion(final String cellName, final int questionID) {
        List<Integer> cells = getCellsIntersectedWithCell(cellName);
        int cellID = getCellID(cellName);
        cells.add(cellID);
        for (int i = 0; i < cells.size(); i++) {
            if (unavialbeQuestions.containsKey(cellID)) {
                unavialbeQuestions.get(cells.get(i)).add(questionID);
            } else {
                List<Integer> lll = new LinkedList<Integer>();
                lll.add(questionID);
                unavialbeQuestions.put(cellID, lll);
            }
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    f(cellName, questionID);
                }
            }, 1000 * 120);
        }
    }

see the function f() that will fired after one hour (in my demo just 2mins), here is the problem, i got null pointer exception when executing the f() function:

protected void f(String cellName, int questionDI) {
        // TODO Auto-generated method stub
        List<Integer> cells = getCellsIntersectedWithCell(cellName);
        int cellID = getCellID(cellName);
        cells.add(cellID);

        for(int i=0;i<cells.size();i++){
            int index = unavialbeQuestions.get(cells.get(i)).indexOf(questionDI);
            unavialbeQuestions.get(cells.get(i)).remove(index);
        }
    }

the exception on the index parameter, what am i doing wrong?

7
  • would help to have the NPE stack trace Commented Jul 6, 2012 at 11:26
  • 1
    It would help to know what line the NPE occurred on. The obvious answer is "you're dereferencing something that's null", but without line numbers it's impossible to say whether getCellsIntersectedWithCell() returned null, or unavialbeQuestions.get(), or cells.get(i), or something else... Commented Jul 6, 2012 at 11:28
  • @assylias i didn't got u, what are you asking me about? Commented Jul 6, 2012 at 11:28
  • @AndrzejDoyle the exception on the index parameter Commented Jul 6, 2012 at 11:29
  • When you receive the NPE (NullPointerException), it shows you the line on which it is thrown - that is an important information because you know where the problem is. Commented Jul 6, 2012 at 11:29

2 Answers 2

2

You don't check that the returned list is not null here:

unavialbeQuestions.get(cells.get(i)).indexOf(questionDI);

and here:

unavialbeQuestions.get(cells.get(i)).remove(index);

And you use different keys for your map here:

if (unavialbeQuestions.containsKey(cellID)) {

and here:

unavialbeQuestions.get(cells.get(i)).add(questionID);

(and your code is not thread-safe, also)

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

9 Comments

unavialbeQuestions.get(cells.get(i)).indexOf(questionDI); for sure it is not null, because i add it in the setUnavialbeQuestion,
the thread will never fire if it is null, check the loop, the tread is begin working in the loop
Read my answer: you set cellID in the map, but get cells.get(i).
@MarkoTopolnik what is the solution please?
To thread-safety? Mark the whole method as synchronized and use a HashMap instead of HashTable.
|
1

unavialbeQuestions or cells list may be null. If you are trying to access it's method, it will throw NullPointerException.

2 Comments

the exception in the index variable
@Totti Just print unavialbeQuestions before the statement unavialbeQuestions.get(cells.get(i)).indexOf(questionDI);

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.