Results 1 to 3 of 3
  1. #1
    cups is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Somehow no object is constructed, cannot find out why!?

    Hey there,

    so I'm trying to get my program to work.
    What I want it to do:
    1) collect information about proteins, protein coding genes and their pathways + reactions in those pathways
    2) create protein, gene, pathway and reaction objects with several attributes
    3) write a file with the collected information (using the toString methods)
    4) use the file to construct new objects with it (in order to, as soon as the first steps are entirely done, be able to create the objects without having to collect the information everytime (it uses parsing and the keggapi, which takes a lot of time))
    5) fill database with the found information

    so.. steps 1-3 work fine, but when I try to make #4 work, I'm getting a null pointer exception, and I'm getting the following exception:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(ArrayList.java:338 )
    at java.util.ArrayList.get(ArrayList.java:351)
    at keggprojekt.KEGGProjekt.erstelleEndObjekte(KEGGPro jekt.java:124)
    at keggprojekt.KEGGProjekt.main(KEGGProjekt.java:30)
    Java Result: 1
    this is the code:

    main method:
    Java Code:
    package keggprojekt;
    
    //import keggprojekt.*;
    import java.util.ArrayList;
    import java.io.*;
    
    public class KEGGProjekt {
    
        public static void main(String[] args) throws Exception {
    
            //Reaktion lisa = new Reaktion("rn:R00014");
    
            String proteinDatei = "/home/lisa/NetBeansProjects/KEGGProjekt/src/keggprojekt/daten/proteindaten.txt";
            DatenDatei protDatei = new DatenDatei(proteinDatei);
    
            // Schreibe UniProt-IDs in Datei
            protDatei.schreibeUniProtIDs();
    
            Protein[] proteine = new Protein[protDatei.getZeilenAnzahl()];
    
            // Erstelle Proteine
            for (int i = 0; i < protDatei.getZeilenAnzahl(); i++) {
                proteine[i] = new Protein(protDatei.getZeile(i));
            }
            System.out.println(protDatei.getZeilenAnzahl() + " Proteine wurden erstellt!");
    
            Gen.schreibeHGNCDatei();
            Protein.schreibeProteinDatei();
            String protDaten = "/home/lisa/NetBeansProjects/KEGGProjekt/src/keggprojekt/daten/proteine.txt";
            erstelleEndObjekte(protDaten);
    
    
        }
    
        private static void erstelleEndObjekte(String datei) throws Exception {
    
            FileReader freader = new FileReader(datei);
            BufferedReader breader = new BufferedReader(freader);
            String zeile;
            ArrayList<Protein> proteinListe;
            ArrayList<Gen> proteinGene;
            ArrayList<Pathway> proteinPathways;
            ArrayList<Reaktion> proteinReaktionen;
    
            /**
             * 62 		=	>     =   PROTEIN
             * 36 		=	$      =   PATHWAY
             * 43 		= 	+      =   GEN
             * 63 		= 	?      =   REAKTION
             * 110          = 	n       =   null
             */
            proteinListe = new ArrayList<Protein>();
            proteinGene = new ArrayList<Gen>();
            proteinPathways = new ArrayList<Pathway>();
            proteinReaktionen = new ArrayList<Reaktion>();
    
            while((zeile = breader.readLine()) != null) {
                int tmp = Character.valueOf(zeile.charAt(0));
    
                switch(tmp) {
                    case 62:
                        System.out.println(":::::::::::::::62:::::::::::::::");
                        String[] tmpprot = zeile.split("\t");
                        Protein tmpProtein;
                        switch(tmpprot.length){
                            case 1:  tmpProtein = new Protein("null", "null", "null", "null");
                                break;
                            case 2: tmpProtein = new Protein(tmpprot[1], "null", "null", "null");
                                break;
                            case 3: tmpProtein = new Protein(tmpprot[1], tmpprot[2], "null", "null");
                                break;
                            case 4: tmpProtein = new Protein(tmpprot[1], tmpprot[2], tmpprot[3], "null");
                                break;
                            case 5: tmpProtein = new Protein(tmpprot[1], tmpprot[2], tmpprot[3], tmpprot[4]);
                                break;
                            default: tmpProtein = new Protein("null", "null", "null", "null");
                                break;
                        }
                        proteinListe.add(tmpProtein);
                        leereListen(proteinGene, proteinPathways, proteinReaktionen);
                        break;
                    case 43:
                        System.out.println(":::::::::::::::43:::::::::::::::");
                        String[] tmpgen = zeile.split("\t");
                        Gen tmpGen;
                        switch(tmpgen.length) {
                            case 0: tmpGen = new Gen("null", "null", "null", "null");
                                break;
                            case 1: tmpGen = new Gen("null", "null", "null", "null");
                                break;
                            case 2: tmpGen = new Gen(tmpgen[1], "null", "null", "null");
                                break;
                            case 3: tmpGen = new Gen(tmpgen[1], tmpgen[2], "null", "null");
                                break;
                            case 4: tmpGen = new Gen(tmpgen[1], tmpgen[2], tmpgen[3], "null");
                                break;
                            case 5: tmpGen = new Gen(tmpgen[1], tmpgen[2], tmpgen[3], tmpgen[4]);
                                break;
                            default:
                                String[] tmpSynonyme = tmpgen[5].split("\t");
                                tmpGen = new Gen(tmpgen[1], tmpgen[2], tmpgen[3], tmpgen[4], tmpSynonyme);
                                break;
                        }
                        Protein letztesProt = proteinListe.get((proteinListe.size() -1));
                        letztesProt.addGen(tmpGen);
                        break;
    
                    case 36:
                        System.out.println(":::::::::::::::36:::::::::::::::");
                        String[] tmppathway = zeile.split("\t");
                        Pathway tmpPathway;
                        switch(tmppathway.length) {
                            case 1: tmpPathway = new Pathway("null", "null", "null");
                                break;
                            case 2: tmpPathway = new Pathway(tmppathway[1], "null", "null");
                                break;
                            case 3: tmpPathway = new Pathway(tmppathway[1], tmppathway[2], "null");
                                break;
                            case 4: tmpPathway = new Pathway(tmppathway[1], tmppathway[2], tmppathway[3]);
                                break;
                            default: tmpPathway = new Pathway("null", "null", "null");
                                break;
                        }
                        Gen letztesGen = proteinGene.get((proteinGene.size()-1));
                        letztesGen.addPathway(tmpPathway);
                        break;
                    case 63:
                        System.out.println(":::::::::::::::63:::::::::::::::");
                        String[] tmpreaktion = zeile.split("\t");
                        Reaktion tmpReaktion;
                        switch(tmpreaktion.length) {
                            case 1: tmpReaktion = new Reaktion("null", "null", "null", "null");
                                break;
                            case 2: tmpReaktion = new Reaktion(tmpreaktion[1], "null", "null", "null");
                                break;
                            case 3: tmpReaktion = new Reaktion(tmpreaktion[1], tmpreaktion[2], "null", "null");
                                break;
                            case 4: tmpReaktion = new Reaktion(tmpreaktion[1], tmpreaktion[2], tmpreaktion[3], "null");
                                break;
                            case 5: tmpReaktion = new Reaktion(tmpreaktion[1], tmpreaktion[2], tmpreaktion[3], tmpreaktion[4]);
                                break;
                            default: tmpReaktion = new Reaktion("null", "null", "null", "null");
                                break;
                        }
                        break;
                    case 110: System.out.println("null ist hier!");
                        System.out.println(":::::::::::::::110:::::::::::::::");
                        break;
                    default: System.out.println("AHH");
                        System.out.println(":::::::::::::::default:::::::::::::::");
                        break;
                }
            }
    
        }
        public static void leereListen(ArrayList<Gen> gene, ArrayList<Pathway> pathways, ArrayList<Reaktion> reaktionen) {
    
            while(gene.size() != 0) {
                gene.remove(0);
            }
            while(pathways.size() != 0) {
                pathways.remove(0);
            }
            while(reaktionen.size() != 0) {
                reaktionen.remove(0);
            }
        }
    }
    class protein:
    (method addGene
    method getGene
    new constructor
    to String)

    Java Code:
    public Protein(String pName, String pUniProt, String pEnsembl, String pEndoNet) {
            name = pName;
            UniProtID = pUniProt;
            UniProtIDkurz = entferneZusatz(UniProtID);
            EnsemblID = pEnsembl;
            EndoNetID = pEndoNet;
            gene = new ArrayList<Gen>();
        }
    
        public void addGen(Gen gen) {
            gene.add(gen);
        }
        public ArrayList<Gen> getGene() {
            return gene;
        }
    public String toString() {
            String genListe = "null";
            if (gene.size() > 0) {
                genListe = gene.get(0).toString();
                for (int i = 1; i < gene.size(); i++) {
                    genListe = genListe + "\n" + gene.get(i);
                }
            }
            return ">>" + "\t" + name + "\t" + UniProtID + "\t" + EnsemblID + "\t" + EndoNetID + "\n" + genListe;
        }
    class gen
    (new constructor
    method addPathway
    method toString)

    Java Code:
    public Gen(String gSymbol, String gName, String gHGNC, String gKEGG, String[] gSynonyme)throws Exception {
            symbol = gSymbol;
            name = gName;
            HGNCID = gHGNC;
            KEGGID = gKEGG;
            ArrayList<Pathway> pathways = new ArrayList<Pathway>();
            synonyme = new ArrayList<String>();
            for(int i=0; i<gSynonyme.length; i++) {
                synonyme.add(gSynonyme[i]);
            }
        }
    
        public void addPathway(Pathway path) {
            pathways.add(path);
        }
     public String toString() {
            String synonymString = "null";
            if(synonyme.size() > 0) {
                synonymString = synonyme.get(0);
                for(int i = 1; i < synonyme.size(); i++) {
                    synonymString = synonymString + ", " + synonyme.get(i);
                }
            }
    
            String pathwayString = "null";
            if(pathways.size() > 0) {
                pathwayString = pathways.get(0).toString();
                for(int i = 1; i < pathways.size(); i++) {
                    pathwayString = pathwayString + "\n" + pathways.get(i).toString();
                }
            }
            return "++" + "\t" + symbol + "\t" + name + "\t" + HGNCID + "\t" + KEGGID + "\t" + synonymString + "\n" + pathwayString + "\n";
        }
    class pathway
    (new constructor
    method toString)

    Java Code:
    public String toString() {
            String reaktionsString = "null";
            if(reaktionen.size() > 0) {
                reaktionsString = reaktionen.get(0).toString();
                for(int i = 1; i < reaktionen.size(); i++) {
                    reaktionsString = reaktionsString + "\n" + reaktionen.get(i);
                }
            }
            return "$$" + "\t" + name + "\t" + pathwayID + "\t" + pathwayLink + "\n" + reaktionsString + "\n";
        }
    
     public Pathway(String pathName, String pathID, String pathLink) {
            reaktionen = new ArrayList<Reaktion>();
            pathwayID = pathID;
            pathwayIDkurz = pathwayID.substring(5, pathwayID.length());
            pathwayLink = pathLink;
            name = pathName;
        }
    class reaction
    (new constructor
    method toString)
    Java Code:
    public Reaktion(String rName, String rID, String rGleichung, String rLink) {
            name = rName;
            reaktionsID = rID;
            gleichung = rGleichung;
            reaktionsLink = rLink;
        }
    public String toString() {
            return "??" + "\t" + name + "\t" + reaktionsID + "\t" + gleichung + "\t" + reaktionsLink;
        }

    this is not everything, but it is everything that's used here.
    I didn't want to spam here.

    the line, the exception is thrown at (124):
    Java Code:
    Gen letztesGen = proteinGene.get((proteinGene.size()-1));
    in main class

    so it does not have a protein constructed yet? I do not know why,
    the System.out.println() in switch
    shows me that it does it in the right order (first the protein, then gene, then pathway..)
    so it should have created one already!
    what am I missing?
    can someone please help? I really don't get the mistake!


    xx cups


    p.s. "leereListen" means "empty Lists" if you've been wondering

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Somehow no object is constructed, cannot find out why!?

    That sure is a lot of code for somebody to wade through. Might want to try shortening it up to an SSCCE.

    Look at line 124 of KEGGProjekt.java. What exactly is it doing? If you aren't sure exactly what values are being passed in, use a debugger or simply add some print statements to figure it out.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default Re: Somehow no object is constructed, cannot find out why!?

    This is nothing to do with no object being constructed.
    proteinGene exists, and is an ArrayList.
    It just happens to be an empty one at that point.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 11
    Last Post: 02-15-2012, 10:13 AM
  2. ClassLoader - can't find class java.lang.Object
    By brenda in forum New To Java
    Replies: 0
    Last Post: 03-30-2011, 04:57 AM
  3. Object cannot find symbol.
    By Acadian in forum New To Java
    Replies: 7
    Last Post: 03-03-2011, 08:33 AM
  4. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  5. Replies: 1
    Last Post: 03-04-2009, 06:14 PM

Posting Permissions

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