Results 1 to 5 of 5

Thread: Java/Processing v 1.2/1.5 coding help

  1. #1
    Join Date
    Dec 2007
    Location
    Sydney, you know. The olympic one.
    Posts
    4,853
    Total Downloaded
    0

    Java/Processing v 1.2/1.5 coding help

    I have my final assignment for comp sci due Mon and having issues resolving a string/array arrangement. Basically am referencing a text file for data (as a string), and calling it into an array. Then splitting the individual lines of the generated arrays into strings.

    I'm getting a null pointer error if I try to reference an index of the array, from all accounts I've written it down on paper and it should be pointing to the right places but can't see the data. If I remove the array reference and hard code the figures it works. I just can't get the reference to work. Have added the code below plus the text as included in the .txt file.

    Thanks if anyone can help.

    CODE
    boolean fills = false;
    int upto;
    int currentGame;
    String puzzle;
    String gameNo;
    String game1Size;
    String[] sizes1; //if the [] is removed a string/array unresolved error occurs
    String game1Dataa;
    String game1Datab;

    void setup () {
    size (300, 300);
    fill (255);
    String[] puzzle = loadStrings("ass3.txt");
    String gameNo = puzzle[0];
    String game1Size = puzzle[1];
    String[] sizes1 = split(game1Size, ' ');
    String game1Dataa = puzzle[2];
    String[] dataa1 = split(game1Dataa, ' ');
    String game1Datab = puzzle[3];
    String[] datab1 = split(game1Datab, ' ');
    }
    //
    void draw() {
    for (int i = 1; i <= int(sizes1[0]); i++) {
    // if a num in inserted into the int( ) there is no null pointer error.
    for (int j = 1; j <= int(sizes1[1]); j++) {
    // if the [ ] is removed it becomes a operator <= error.
    rect (j+j*20, i+i*20, 20, 20);
    }
    }
    }

    void mousePressed (){
    for (int i = 1; i<= 10; i++) {
    for (int j = 1; j<=10; j++) {
    if (mouseX > j+j*20 && mouseX < j+(j+1)*20 && mouseY >i+i*20 && mouseY < i+(i+1)*20){
    println (mouseX + ", " + mouseY);

    fills = !fills;
    if (fills) {
    fill(0);
    } else {
    fill(255);
    }
    }
    }
    }
    }
    TEXT
    3
    5 5
    5 2 3 2 5
    5 2 3 2 5
    8 11
    2 2 7 7 11 9 4 4
    3 2 6 5 5 4 5 5 6 2 3
    1 1
    1
    1

  2. #2
    Join Date
    Aug 2006
    Location
    Melbourne
    Posts
    34
    Total Downloaded
    0
    CODE
    boolean fills = false;
    int upto;
    int currentGame;
    String puzzle;
    String gameNo;
    String game1Size;
    String[] sizes1; //if the [] is removed a string/array unresolved error occurs
    String game1Dataa;
    String game1Datab;

    void setup () {
    size (300, 300);
    fill (255);
    String[] puzzle = loadStrings("ass3.txt");
    String gameNo = puzzle[0];
    String game1Size = puzzle[1];
    String[] sizes1 = split(game1Size, ' ');
    String game1Dataa = puzzle[2];
    String[] dataa1 = split(game1Dataa, ' ');
    String game1Datab = puzzle[3];
    String[] datab1 = split(game1Datab, ' ');
    }
    You seem to be declaring your variables twice. Once in global space and once local to the setup function.

    I'm assuming it crashes with the null pointer in the draw routine...

    You assign values in the setup function to the local vars but access the global unassigned ones in the draw funtion.

    Remove all the String and String[] type declarations from your setup funct and see how you go...

  3. #3
    Join Date
    Aug 2006
    Location
    Melbourne
    Posts
    34
    Total Downloaded
    0

    Thumbs up

    Fresh eyes in the morning see that its just the sizes1 array double declare is your problem.

    Although I would take out the global declarations for the other vars you only use in the setup routine.. This will save you and others getting mixed up as to whats what

    Another thing I do is name class vars with a a starting m_ (ie m_sizes1[]) , global ones with g_ and local tmp ones can be plain like you have done..

    You can then see at a glance which scope the var is in .. and it also stops you accidently using the same var name twice. Handy for functions too..

    void setSizes(String sizes[])
    {
    // you can see at a glance that you
    // are setting the class var//
    m_sizes = sizes;
    }



    Hope this helps

  4. #4
    Join Date
    Dec 2007
    Location
    Sydney, you know. The olympic one.
    Posts
    4,853
    Total Downloaded
    0
    If I don't declare the Strings globally but only in setup or draw it runs other errors. If I declare and assign globally they go wack.

    We cant use OOP, classes etc. Just strings/arrays.

    Just made it work by putting the refs in void draw. Works for a single ref, just have to make it scroll through multiple games.


    boolean fills = false;
    int upto;
    int currentGame;
    String puzzle;
    String gameNo;
    String game1Size;
    String[] sizes1;
    String game1Dataa;
    String game1Datab;

    void setup () {
    size (300, 300);
    fill (255);
    }

    void draw() {
    String[] puzzle = loadStrings("ass3.txt");
    String gameNo = puzzle[0];
    String game1Size = puzzle[1];
    String[] sizes1 = split(game1Size, ' ');
    String game1Dataa = puzzle[2];
    String[] dataa1 = split(game1Dataa, ' ');
    String game1Datab = puzzle[3];
    String[] datab1 = split(game1Datab, ' ');
    for (int i = 1; i <= int(sizes1[0]); i++) {
    for (int j = 1; j <= int(sizes1[1]); j++) {
    rect (j+j*20, i+i*20, 20, 20);
    }
    }
    }

    void mousePressed (){
    for (int i = 1; i<= 10; i++) {
    for (int j = 1; j<=10; j++) {
    if (mouseX > j+j*20 && mouseX < j+(j+1)*20 && mouseY >i+i*20 && mouseY < i+(i+1)*20){
    println (mouseX + ", " + mouseY);

    fills = !fills;
    if (fills) {
    fill(0);
    } else {
    fill(255);
    }
    }
    }
    }
    }
    I'm running a keypressed fnct as:

    void keyPressed() {
    if (key == 'n') {
    currentGame = currentGame++;
    }

    if (key == 'p') {
    currentGame = currentGame--;
    }
    }
    I'm think I'll need a loop to count through the game no's but how do I correlate a hard coded no (1,2,3) to relate to an array index (where the game size is declared) I know a loop will need to cycle in counts of 3 as there are three lines in the txt file per game .

    for(i = 0, i,= 9; i + 3 will get me a 3 count yes?

    but the next line will need to be a ref to the next array index referencing the size required (current index + 3)

  5. #5
    Join Date
    Aug 2006
    Location
    Melbourne
    Posts
    34
    Total Downloaded
    0
    PHP Code:
        int         m_currentDataLine;    // master data line index
        
    String[]     m_fileData;            // Master data read in by loadStrings

        // Pull one line string from master data array
        // and inc currentline index
        
    String getNextLine()
        {
            
    String retLine null;
            if(
    null != m_fileData)
            {
                if(
    m_currentDataLine m_fileData.length)
                {
                    
    retLine m_fileData[m_currentDataLine];
                    
    m_currentDataLine++;
                }
            }
            return 
    retLine;
        }

        
    void setup()
        {
            
    m_currentDataLine 0;

            
    // Load up data arrya
            
    m_fileData loadStrings("ass3.txt");
        }

        
    void draw()
        {
            
    // Do i have some data
            
    if(null != m_fileData)
            {
                
    //Yep! process it....

                // Reset current master data line
                
    m_currentDataLine 0;

                
    // Get num games line from file data loaded in setup
                
    String currLine readNextLine();

                
    // Convert num games string to an integer type
                
    int numGames Integer.valueOf(currLine);

                
    // Loop through games
                
    for(int i 0numGames;i++)
                {
                    
    System.out.println("Game number " +1);

                    
    // Read game header line
                    
    currLine readNextLine();

                    
    // Get individual data line element counts
                    
    String[] datalines split(currLine' ');

                    
    // Get number of data lines
                    
    int numDataLines datalines.length;

                    
    // Now process the data lines
                    
    for(numDataLines;i++)
                    {
                        
    // Read in the data line  elements;
                        
    currLine readNextLine();

                        
    // Split the elements
                        
    String[] dataElements split(currLine' ');

                        
    // Get number of elements for current data line
                        
    int numElements Integer.valueOf(dataLines[j]);
                        
    // could just as easily be
                        // int numElements = dataElements.length;

                        // Process elements
                        
    for(0numElements;n++)
                        {
                            
    // Get each element
                            
    int element Integer.parseInt(dataElements[n]);

                            
    // Do whatever with data element
                            
    System.out.println(" Data element = " element);
                        }
                    }
                }
            }
        } 
    You need to keep track of what line you are upto from the master data.
    And convert you Strings to integers where necessary

    Hard to explain over a post ... but here is an example based on your code
    I have not compiled it .. so maybe some typo's in there... and hardly any error checking

    This example will
    - Load the master data once
    - Each call to draw
    Reset the data line to the start
    Process the whole master data printing out the game id and elements

    Ps not really php code code.. can't seem to post formatted text any other way

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search AULRO.com ONLY!
Search All the Web!