Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am working on a simple system that collects elements from an array that is chosen through a series of button events, copies it into a new array called 'oneRay' and proceeds to cycle through its elements to test the user on their knowledge. If a user correctly identifies an answer from a prompt, the first element of the 2D array in that section becomes "", and is skipped with this code:

        do{
        pick = random.nextInt(oneRay.length);
    }while(!(oneRay[pick][0].equals("")));

Which continues to return the aforementioned exception. The following code creates random as a member of the class in question:

Random random = new Random()

So what could I be doing wrong? Any help will be greatly appreciated.

UPDATE: Yes the elements are initialized through a series of switch statements that direct proper the selected array to oneRay

The error report is huge as it is launched as I am trying to build the swing UI, so it tosses failures left and right

Here is what I used for initialization:

switch(subject){
    case 1:
        switch(unit){
        case 1: oneRay = new String[Master.SciChemArray_IntroductionANDFirstChapter.length][2];
           for(int i = 0; (Master.SciChemArray_IntroductionANDFirstChapter.length) > i; i++){
               oneRay[i][0] = Master.SciChemArray_IntroductionANDFirstChapter[i][0];oneRay[i][1] = Master.SciChemArray_IntroductionANDFirstChapter[i][1];}
        default: System.exit(0); break;
        } break;
    case 2:
        switch(unit){
        case 1: 
            oneRay = new String[Master.MathaRay_Part2.length][2];
           for(int i = 0; (Master.MathaRay_Part2.length) > i; i++){
               oneRay[i][0] = Master.MathaRay_Part2[i][0]; oneRay[i][1] = Master.MathaRay_Part2[i][1];} break;
        default: System.exit(0); break;
        } break;

When creating this setup I pass a number which corresponds to a subject and a unit number, which are then used to copy the relevant array like so ^^

Unit and Subject are passed like so:

         mathstatb1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent l) {
            new SwingImplementation(2, 1);
        }}); 

Whereby a button sends off subject 2 and unit 1

And here are the Errors, now at the bottom, if anyone really wants them:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at wbh.SwingImplementation.<init>(SwingImplementation.java:60)
at wbh.matstatMenu$1.actionPerformed(matstatMenu.java:23)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
share|improve this question
    
Has oneRay and its elements been initialized? –  Sotirios Delimanolis Sep 21 '13 at 20:06
    
I need to see all the code and the whole exception report. –  tbodt Sep 21 '13 at 20:06
    
It's obviously not initialized if you are getting NPE. Post your relevant code. –  Sotirios Delimanolis Sep 21 '13 at 20:10
    
did you initialize oneRay to null before the switch case? –  Gaurav Varma Sep 21 '13 at 20:24

2 Answers 2

Try to make sure that all of the oneRay array values are initalized correctly.

The following line seems most prone for NullPointer. If you try to call .equals() method for null it throws the NullPointer.

oneRay[pick][0].equals("");
share|improve this answer
    
I cannot see why there would be a null though if my for-loop fills all properly from an array with no null-s; thanks for the contribution I will double check my methods –  William Brun Sep 21 '13 at 20:17
1  
@williambrun Either attach a break point in your code and examine the value or add a System.out to dump the value. Your perception and reality don't match, and reality is winning... –  MadProgrammer Sep 21 '13 at 20:32

It would be more clear if you paste the whole code. In current code, I see the following might be throwing a nullpointer :

!(oneRay[pick][0].equals(""))

change it to

!("".equals(oneRay[pick][0]))
share|improve this answer
    
if oneRay[pick] is not initialized, this will still NullPointer... –  Quirliom Sep 21 '13 at 20:10
1  
those two statements are the same –  redFIVE Sep 21 '13 at 20:14
    
no they are not.. if oneRay[pick][0] is null it will throw a nullpointer while calling equals... but when equals is called for "" it will never throw nullpointer –  Gaurav Varma Sep 21 '13 at 20:16
    
it will cause a null pointer either way –  redFIVE Sep 21 '13 at 20:17
    
try that code in your program.. will clarify it –  Gaurav Varma Sep 21 '13 at 20:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.