0

For an assignment I've come across an Issue. For a bit of a boost I'll tell you what it is I'm working on.

The assignment requires me to create a java applet (Even though I'm using a JFrame, oops) that gets input from the user in the form of a piece of text. With this text, the program will how many of each length of word there are and display the results in the form of a graph in a separate window. My hashmap contains two Integers, the length of the word and how many of that length there is.

However the assignment requires that the user be able to upload two text files and compare the results, therefore I have to send an array of hashmaps, each with separate results to the window. I have been doing that this way:

public void dispatchNewGraph (ArrayList<Map<Integer, Integer>> sortedCount) {
    TextAnalyserGraph graphFrame = new TextAnalyserGraph(sortedCount);
}

and in the window that opens, the data passed to the constructor is applied to this property in the TextAnalyserGraph class private ArrayList<Map<Integer, Integer>> graphData;

My issue is that I'm getting a nullpointerexception error when I try to iterate this Array of HashMaps with the following code:

for(Iterator<Map<Integer, Integer>> i = graphData.iterator(); i.hasNext(); ) {
    Map<Integer, Integer> graph = i.next();
    for (Integer value : graph.keySet()) {
        if(graph.containsKey(value)) {
            Integer v = graph.get(value);
            if (v.intValue() > largest)
                largest = v.intValue();
        }
        bars++;
    }
}

The error starts at the first line of that last code block. If I move graphData to the next line, the same error starts point there indicting an issue with graphData., I'm really struggling to make progress on this!

Thanks very much! :)

Stacktrace

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at TextAnalyserGraph.paint(TextAnalyserGraph.java:47)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:819)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
    at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
8
  • 1
    However the assignment requires that the user be able to upload two text files and compare the results, therefore I have to send an array of hashmaps, each with separate results to the window. If you know there is only 2 Maps, why do you need an array? Why not use a Pair, or pass the 2 maps as separate arguments. Commented Mar 6, 2014 at 20:40
  • 1
    The stacktrace doesn't help too much when we don't know the line numbers :) Commented Mar 6, 2014 at 20:42
  • @Gergy008 - what code is at like (TextAnalyserGraph.java:47)? Commented Mar 6, 2014 at 20:42
  • Because they might not upload both files, the user has the option to load one or two or more I guess. The error starts at the first line of that last code block, which would be for(Iterator<Map<Integer, Integer... Commented Mar 6, 2014 at 20:44
  • When you use your debugger, what is the value of graphData at the point just before the exception happens? Commented Mar 6, 2014 at 20:45

2 Answers 2

1

You need to do a null check on graphData before looping.

I would also suggest modifying your for loops to be a little nicer.

You could use this if you care about the keys:

        if(graphData != null){
            for(Map<Integer, Integer> valueMap : graphData){
                for (Map.Entry<Integer, Integer> entry : valueMap.entrySet()) {
                    if (entry.getValue() > largest)
                        largest = entry.getValue();
                    bars++;
                }
            }
        }

But since you do not care about key and it's ever used in your looping logic, I would suggest:

    if(graphData != null){
        for(Map<Integer, Integer> valueMap : graphData){
            for (Integer value : valueMap.values()) {
                if (value > largest)
                    largest = value;
                bars++;
            }
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Credit to David also, but an excellent response nevertheless. Thanks for your help guys, sometimes it just takes another person to provide insight :)
That's what we're here for :)
0

Unless you have 100% control over the input, you'll want to check if graphData is null before requesting the iterator, then check if graph is null before requesting the keyset.

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.