Results 1 to 5 of 5
  1. #1
    Riftwalker is offline Member
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default [SOLVED] Null Pointer Exceptions

    Hi. I have code that runs in a program designed to parse a formatted file and display output. Now I am taking that parsing and putting it into a different program that's supposed to parse the same type of formatted file but this time it puts the information it pulls out into a .txt file. The only thing is in the new program I'm running into null pointer exceptions and when I use try and catch for those it puts out a blank file. Here is the code without the try and catch statements in it.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import java.io.File;
    import javax.swing.border.*;
    import java.awt.event.*;
    import java.io.FileNotFoundException;
    import java.lang.SecurityException;
    import java.util.Formatter;
    import java.util.FormatterClosedException;
    import java.util.NoSuchElementException;
    import java.util.Scanner;
    
    public class ParseArrays
    {
        private Formatter output;
        private Contig[] contigArray;
        private SummaryLine[] summaryArray;
        
        public void setContigArray(Contig[] sca)
        {
            contigArray = sca;
            summaryArray = new SummaryLine[contigArray.length];
        }
        
        public void analyzeFile()
        {    
    		  for(int a = 0; a < contigArray.length; a++)
            {
                String backbone = contigArray[a].getContigSeq();
                interactiveRectangle[] rectArray = contigArray[a].getRectArray();
                String[] contig = new String[backbone.length()];
                int[] scoresArray = contigArray[a].getScoresArray();
                int[] numberOfAllelesArray = new int[backbone.length()];
                String[] allelesArray = new String[backbone.length()];
                int[] totalAllelesArray = new int[backbone.length()];
                for(int b = 0; b < backbone.length(); b++)
                {
                    contig[b] = String.valueOf(backbone.charAt(b));
                    numberOfAllelesArray[b] = 0;
                    allelesArray[b] = "";
                    totalAllelesArray[b] = 0;
                }      
    			   for(int c = 0; c < rectArray.length; c++)
                {
                    int temp1 = rectArray[c].getStartOfUsedRegion();
                    int temp2 = rectArray[c].getEndOfUsedRegion();
                    String rectSeq = rectArray[c].getContigSeq();
                    String[] rect = new String[backbone.length()];
                    for(int d = 0; d < backbone.length(); d++)
                    {
                        if((temp1-1 <= d) && (d < temp2))
                        {
                            rect[d] = String.valueOf(rectSeq.charAt(d));   
                        }
                        else
                        {
                            rect[d] = "";
                        }
                    }
    			       if(temp2 > backbone.length())
    					 {temp2 = backbone.length();}
                    for(int e = temp1-1; e < temp2; e++)
                    {
                        totalAllelesArray[e] += 1;
                        if(!rect[e].equals(contig[e]))
                        {
                            numberOfAllelesArray[e] += 1;
                            allelesArray[e] = allelesArray[e] + rect[e];
                        }
                    }
                }
                summaryArray[a].setContigName(contigArray[a].getContigName());
                summaryArray[a].setNumberOfAlleles(numberOfAllelesArray);
                summaryArray[a].setAlleles(allelesArray);
                summaryArray[a].setContigSeq(contig);
                summaryArray[a].setTotalAlleles(totalAllelesArray);
            }
        }
        
        public void openFile(String fileName)
        {
            fileName += " Summarized Results.txt";
            try
            {
                output = new Formatter(fileName);   
            }
            catch(SecurityException se)
            {
                System.err.println("You do not have access to this file.");
                System.exit(1);
            }
            catch(FileNotFoundException fnfe)
            {
                System.err.println("Error creating file.");
                System.exit(1);
            }
        }
        
        public void addRecords()
        {     
            for(int f = 0; f < summaryArray.length; f++)
            {   
                String finalOutput = "";
                String tempContigName = summaryArray[f].getContigName();
                int[] tempNumberOfAlleles = summaryArray[f].getNumberOfAlleles();
                String[] tempAlleles = summaryArray[f].getAlleles();
                String[] tempContigSeq = summaryArray[f].getContigSeq();
                int[] tempTotalAlleles = summaryArray[f].getTotalAlleles();
              
                for(int g = 0; g < tempContigSeq.length; g++)
                {
                    if(tempNumberOfAlleles[g] != 0)
                    {
                        finalOutput = finalOutput + tempContigName + "\t";
                        finalOutput = finalOutput + String.valueOf(g+1) + "\t";
                        finalOutput = finalOutput + String.valueOf(tempNumberOfAlleles[g]) + "\t";
                        finalOutput = finalOutput + String.valueOf(tempTotalAlleles[g]) + "\t";
                        finalOutput = finalOutput + tempContigSeq[g] + "\t";
                        finalOutput = finalOutput + tempAlleles[g];
                    }
                }
                try
                {
                    output.format("%s\n", finalOutput);
                }
                catch(FormatterClosedException fce)
                {
                    System.err.println("Error writing to file.");   
                }
                catch(NoSuchElementException nsee)
                {
                    System.err.println("Invalid input. Please try again.");
                    return;
                }
            }
        }
        
        public void closeFile()
        {
            if(output != null)
            {
                output.close();
            }
        }
    }
    Thanks for any help.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I'm running into null pointer exceptions
    Please post the full text of the error messages.

    Change your code to print the stack when you get exceptions:
    fce.printStackTrace();
    The trace will show you what statement the error occured on.
    Then look at that statement to see what object is null.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Riftwalker is offline Member
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default

    Thanks everyone. I did find out where and why the pointer was pointing to something null. My program is now running correctly.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If you are using any IDE for coding, debug and see on which line you get the null value. If you are not using any IDE for coding, print the stack trace and see what happen. Just a hint!

    If you have solve the problem please mark the thread as solved. :)

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM
  3. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM
  4. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 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
  •