Results 1 to 5 of 5
  1. #1
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default Just wondering if somebody could check my IF ELSE statement ?

    Hi guys, Just wondering if somebody could check my IF ELSE statements as it seems to completely skip part of my code!

    Sorry about the long piece of code. Basically I open i file dialog box, then open a txt file. Read the data from it. If it is a blank line skip that line, if the line is a // line skip it, but when it reaches a flag "[ " that data should be stored as appropriate.

    Thanks for any help!

    Java Code:
    public void readData()
          {
              FileDialog fileDialog = new FileDialog(mainWindow, "Open", FileDialog.LOAD);
              fileDialog.setVisible(true);
              filename = fileDialog.getFile();
              //Loads a file dialog window for the user to select a file
              
          try {
              //Preparing a Scanner that will scan the document
              Scanner input = new Scanner(new File (filename));
     
              
            while( input.hasNextLine() )//Read each line of the file
            {
                   lineOfText = input.nextLine(); //Reads a line of text
                   AudioVisual av;
                   typeOfData = null;
    
                   if (! lineOfText.isEmpty() && ! lineOfText.startsWith("//"))  
                  { 
                      //Ignore
                    }
                  
                  else if (lineOfText.startsWith("[")) //If this line is a flag
                  {  
                      if (lineOfText.equalsIgnoreCase("[CD data]"))
                      typeOfData = "CD"; //Stores type of data to be CD
                      else if (lineOfText.equalsIgnoreCase("[DVD data]"))
                      typeOfData = "DVD"; //Stores type of data to be DVD
                      else if (lineOfText.equalsIgnoreCase("")){
                      System.out.println("Unknown data type!"); //Deal with unknown type not sure if this is right
                      }                    
                                    input.useDelimiter("[ ]*(,) [ ]*"); //Use delimiter
                      
                   }
                   
                   else if (typeOfData.equals("CD"))//Checks to make sure its CD data//When it gets to this line it skips to catch and gives NullPointerException
                      {
                        av = new CD();
                        av.extractTokens(input); //Extracts the tokens
                        addItem(av);
                      }
                    
                   else if (typeOfData.equals("DVD")) //Checks to make sure its DVD data
                     {
                        av = new DVD();
                        av.extractTokens(input); //Extracts the tokens
                        addItem(av);
                     }
                   
                    else
                     {
                      typeOfData = null; //Not sure about this line
                     }  
                      
    
            }
                  input.close();//Closes 2nd scanner 
    
          }catch (Exception e){e.printStackTrace();}
       }
    Last edited by sim18; 03-27-2012 at 09:58 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Just wondering if somebody could check my IF ELSE statement ?

    Please use [code] tags [/code] when posting code, especially large chunks of it like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Just wondering if somebody could check my IF ELSE statement ?

    Java Code:
    while () {
       typeOfData = null;
    ...
       else if (typeOfData.equals("CD"))
    ...
    }
    Nowhere in between that declaration and that 'else if' do you instantiate typeOfData, so it is always null if you fall through to that bit of your if..else if...else block.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default Re: Just wondering if somebody could check my IF ELSE statement ?

    Do you mean moving typeOfData = null, further down in my code say to line 26, or that line 37's else if is wrong ?

    Thank you

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Just wondering if somebody could check my IF ELSE statement ?

    I have no idea, since I have no idea what this is supposed to do.
    But that is the reason you are getting a null pointer exception.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2012, 04:09 AM
  2. Replies: 2
    Last Post: 09-25-2011, 02:48 AM
  3. Wondering what I need to learn to make web apps
    By silverglade in forum New To Java
    Replies: 10
    Last Post: 06-10-2011, 01:10 AM
  4. Wondering..~
    By ryozkidz in forum Java Applets
    Replies: 0
    Last Post: 01-20-2011, 10:09 PM
  5. Hi, i was wondering if someone could help me
    By deathnote202 in forum New To Java
    Replies: 4
    Last Post: 04-23-2010, 05:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •