We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › [SOLVED] object communication problems
Page Index Toggle Pages: 1
[SOLVED] object communication problems (Read 785 times)
[SOLVED] object communication problems
Dec 5th, 2009, 5:12pm
 
Hello all! I have made a program which reads out text files and key input and displays it in app window, the key input can be read by a (not fully implemented) parser which in turn executes commands. Think of it of a really low end version of a terminal or a dos like program. I am making this system object oriented for ease of use. But I am running in some problems, the text read out works fine but keypress handler crashes after a single keypress (EDIT: stack trace in next post). My code is getting a bit muddled and have little experience with OOP, I am having some trouble seeing were I went wrong. Main controller and classes pasted below. Sorry for the long post, but would appreciate any input.

int leftmargin = 10;
float fSize = 16;
int maxLength = 65;
TextEngine sendLine = new TextEngine();
TextEngine displayText = new TextEngine();
KeyInput newKey = new KeyInput();
Parser newParse = new Parser();
//int row = 0;

void setup()
{
 size(600,400);
 background(0);
 fill(0,255,0);
 textFont(loadFont("DOS16.vlw"),fSize);
 //displayText.updateText(loadStrings("test.txt"));
}

void draw(){}

void keyPressed()
{
 char k;
 k = (char)key;
 newKey.update(k);
 println("key pressed");
}

/*
             code below meant for second tab
*/

class Parser
{
 String tempLine;
 Boolean cmdSent;
 Boolean lastLetter;
 String inputString;
 Parser()
 {
   tempLine = "";
   cmdSent = false;
   lastLetter = false;
 }
 void sendParse(String input)
 {
   inputString = input;
   inputString = inputString.toLowerCase();
   for (int i = 0; i < inputString.length(); i++)
   {
    //println("char " + i);
    char outLetter = inputString.charAt(i);
    if (i == inputString.length()-1)
    {
      lastLetter = true;
      //println("last letter " + outLetter);
      charChecker(outLetter);
    } else {
      charChecker(outLetter);
    }
  }
}

void charChecker(char inLetter)
{
 //println("char pass");
 if (lastLetter == true)
 {
   //println("trigger last");
   if (inLetter != 32)
   {
     tempLine = tempLine + inLetter;
   }
   cmdNounSplitter();
   lastLetter = false;
   } else {
     switch(inLetter)
     {
     case 32: // space
     cmdNounSplitter();
     break;
     case 8: //backspace
     case 10: //enter pc
     case 13: // enter mac
     case 65535:
     case 127:
     case 27:
     break;
     default:
     tempLine = tempLine + inLetter;
     //print(tempLine);
     break;
     }
   }
 }

 void cmdChecker(String inputLine)
 {
   if(inputLine.equals("cd"))
   {
     println("cd command");
   } else if(inputLine.equals("run"))
   {
     println("run command");
   } else {
     println("error");
   }  
   tempLine = "";
 }

 void nounChecker(String inputLine)
 {
   if(inputLine.equals("bla"))
   {
     println("bla noun!");
   } else if (inputLine.equals("text.txt"))
   {
     println("text noun!");
   } else {
     println("error");
   }
   tempLine = "";
   cmdSent = false;
 }

 void cmdNounSplitter()
 {
   if (tempLine.equals("") == false)
   {
     //println("not empty");
     if (cmdSent == false)
     {
       //println("cmdSent !");
       //println(tempLine);
       cmdSent = true;
       cmdChecker(tempLine);
     } else {
       //println("noun sent!");
       //println(tempLine);
       nounChecker(tempLine);
     }
   }
 }
}
class TextEngine
{
 Boolean moreText;
 int nrStartChar;
 int numRow;
 int numLines;
 int row;
 String[] lineBuff;
 TextEngine()
 {
   moreText = false;
   nrStartChar = 0;
   numRow = 25;
   row = 0;
   lineBuff = new String[numRow];
 }
   void updateText(String lines[])
 {
   background(0);
   //println("there are " + lines.length + " lines");
   println(lines.length);
   numLines = lines.length;
   for (int i=0; i < lines.length; i++)
   {
   //println(lines[i]);
     if (i < numRow-1)
     {
       lineBuff[i] = lines[i];
     } else {
       moreText = true;
       println("there is more text");
     }
   }
   //lineBuff[row] = "";
   float uPos;
   char k;
   //println(row);
   //uPos = textWidth(lineBuff[row]) + leftmargin;
   int maxLines;
   if(numLines > numRow)
   {
     maxLines = numRow -1;
     } else {
       maxLines = numLines;
     }
   for(int i = 0; i < maxLines; i++)
   {
     println("going " + i + " times");
     text(lineBuff[i],leftmargin,(i*fSize)+fSize);
     row = row + 1;
   }
 }
}

class KeyInput
{
 String[] activeInput;
 KeyInput()
 {
   activeInput = new String[1];
   activeInput[0] = "";
 }
 void update(char m)
 {
 switch(m)
   {
     case 8:
     if (activeInput[0].length() > 0)
    {
      println("backspace");
      activeInput[0] = activeInput[0].substring(0, activeInput[0].length()-1);
      println(activeInput[0]);
      sendLine.updateText(activeInput);
    }
     break;
     case 10:
     case 13:
     String activeString = activeInput[0];
     newParse.sendParse(activeString);//send to parser!
     break;
     case 65535:
     case 127:
     case 27:
     break;
     default:
     if(activeInput[0].length() < maxLength)
     {
       activeInput[0] = activeInput[0] + m;
       sendLine.updateText(activeInput);
     }
     break;
     }
   }
 }


Re: object communication problems
Reply #1 - Dec 6th, 2009, 12:20am
 
If you include the stacktrace of your crash, it's a lot easier to help.



What's the point of this code?

activeInput[0] = "";
activeInput[0] = activeInput[0].substring(0, activeInput[0].length()-1);

This looks like a possible cause of the crash...
Re: object communication problems
Reply #2 - Dec 6th, 2009, 2:33am
 
"would appreciatie any input" Lol! Wink

JR is right in the sense you should check if you have some characters in your input before deleting them, but as long as you don't hit backspace, there shouldn't be a problem.
I too fails to see why you create an array of strings and use only one item of the array. If you plan to have an history of commands, you should put activeInput as member of the class, not as local variable of the method. Otherwise you won't store any character there.

"null point exception somewhere in the keypress and/or textengine classes"
Again JR is right: these exceptions show a detailed stack trace so we can know the exact location of the issue. Your vague report doesn't help... Smiley
Re: object communication problems
Reply #3 - Dec 6th, 2009, 5:20am
 
Problem at input, lol indeed ; ). I updated my first post to reflect the change in code:      
class KeyInput
{
 String[] activeInput;
 KeyInput()
 {
   activeInput = new String[1];
   activeInput[0] = "";
 }
 switch(m)
   {
     case 8:
     if (activeInput[0] != null && activeIput[0] != "")
    {
       activeInput[0] = activeInput[0].substring(0, activeInput[0].length()-1);
       sendLine.updateText(activeInput);
    }
This undoubtably solves one of my problems, but I still have a null pointer error. The reason for the one line array is because my TextEngine class works with arrays, and I don't want to make a exception for keyinput (because my loadtext command works with arrays). But maybe there is a more elegant way of doing this. I should have added the stack trace to the first post, but I didn't want to make the post longer still... I'll correct this by adding the error ouput below.

cd command
text noun!
1
going 0 times
key pressed
1

Exception in thread "Animation Thread" java.lang.NullPointerException
     at processing.core.PGraphics.textWidth(PGraphics.java:2583)
     at processing.core.PApplet.textWidth(PApplet.java:7302)
     at Terminal_deceitObject6$TextEngine.updateText(Terminal_deceitObject6.java:191)
     at Terminal_deceitObject6$KeyInput.update(Terminal_deceitObject6.java:237)
     at Terminal_deceitObject6.keyPressed(Terminal_deceitObject6.java:41)
     at processing.core.PApplet.handleKeyEvent(PApplet.java:1752)
     at processing.core.PApplet.dequeueKeyEvents(PApplet.java:1735)
     at processing.core.PApplet.handleDraw(PApplet.java:1437)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:619)
Re: object communication problems
Reply #4 - Dec 6th, 2009, 5:38am
 
I guess the problem occurs here:
uPos = textWidth(lineBuff[row]) + leftmargin;

... and you probably haven't initialized that position in the array.
Re: object communication problems
Reply #5 - Dec 6th, 2009, 5:55am
 
Hmmn your right, removing it fixes the error. But although the uPos variable is not used for anything now, I want to use to add a blinking _ later on. I am not sure how to initialize that part of the array, because it already should be filled with my text input. Whats going wrong here? Thanks for the help so far guys. Also this seems more sensible:
     case 8:
     if (activeInput[0].length() > 0)
    {
      println("backspace");
       activeInput[0] = activeInput[0].substring(0, activeInput[0].length()-1);
       sendLine.updateText(activeInput);
    }

EDIT: Jay! Got parser to work as well, updated code in first post.
EDIT: solved the problem with uPos as well, needed to put in an if statement checking for null content of the string, works now! Perhaps a bit redundant but going to put the topic up as solved.
Page Index Toggle Pages: 1