开发者

Object variable might not have been initialized

I cannot find the error that is being generated at right.GetText(); near the bottom. The error is the title above. From what i've read, member variables have to be initialized in Java, so I went throught and looked at all of those to no avail. I think i've done everything correctly in my default constructor, but I may be wrong, this my first java program. Thanks for the help, I just don't want to spend another hour on this.

import java.io.*;
import java.lang.*;

//class Shapes 
//{ 
    class Triangle
    {
        private int rowNum;
        private String text;

        Triangle() {
            rowNum=0;
            text="";
        }

        public void GetText()
        {
            int flag=0;
            String trialText="";

            while(flag==0){
                BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));

                System.out.println("\nPlease enter a word to display in the triangle(ie bob, tom, etc.)");

                try{ 
                    trialText = reader.readLine(); 
                    System.out.println("Your entered "+ trialText); //print the data entered by the user 
                }
                catch (IOException ioe){ //statement to execute if an input/output exception occurs 
                    System.out.println("An unexpected error occured."); 
                    continue;
                }

                char[] newText=trialText.toCharArray();

                for(int i=0;i<trialText.length();i++){
                    if(!Character.isLetter(newText[i])){
                        System.out.println(newText[i]+"is not a letter, please enter a real word.");
                        flag=0;
                        break;
                    }

                flag=1;
                }

            }//While(flag==0)
            text=trialText;
            text=text.toLowerCase();     //Converting all input text to Lower Case
        }

        public void ShowText()
        {
            System.out.println(text);
        }

        public void ShowRowNum()
        {
            System.out.println(rowNum);
        }
        /*
        public boolean testText(String new_Text)
        {
            try{ 
                String trialText = new_Text;
                System.out.println("You entered "+trialText); //print the data entered by the user 
            }
            catch (IOException ioe){ //statement to execute if an input/output exception occurs 
     开发者_开发百科           System.out.println("An unexpected error occured."); 
                return false;
            }
            return true;

        }
        */  
        public boolean testRowNum(String new_Text)
        {
            try{ 
                Integer.parseInt(new_Text);
                System.out.println("You entered "+ new_Text); //print the data entered by the user 
            }
            catch (NumberFormatException nfe){ //statement to execute if an input/output exception occurs 
                System.out.println("You have entered a non-integer. "); 
                return false;
            }
            return true;
        }
        public void GetRows()
        {
            System.out.println("Please enter the number of rows the triangle will contain");

            BufferedReader newRow= new BufferedReader(new InputStreamReader(System.in));
            try{
                if(testRowNum(newRow.readLine().trim())==true)
                    rowNum=Integer.parseInt(newRow.readLine().trim());
            }
            catch (IOException ioe) {//statement to execute if an input/output exception occurs 
            System.out.println("You have entered a non-integer. "); 
            }
        }

        public void DisplayTriangle()
        {
            int numSpaces=0;
            String spaces="";  
            String bricks="";

            for(int i=0; i<rowNum; i++){
                if(rowNum%2==0)
                    numSpaces=(rowNum/2);
                else if(rowNum%2==1)
                    numSpaces=((rowNum/2)-1);
                for(int j=numSpaces; j>0; j--){
                    spaces=spaces+" ";
                }

                for(int k=0; k<rowNum; k++){
                    bricks=bricks+"=";
                }

                System.out.println(spaces+bricks);
            }
        }

        public static void main(String args[])
        {
            Triangle right;

            right.GetText();
            right.GetRows();
            right.ShowText();
            right.ShowRowNum();
            right.DisplayTriangle();
        }
    }
//}


try this out in ur main fn:

Triangle right = new Triangle();

you have to initialize the Triangle


Try this:

public static void main(String args[])
        {
            Triangle right = new Triangle();

            right.GetText();
            right.GetRows();
            right.ShowText();
            right.ShowRowNum();
            right.DisplayTriangle();
        }

You tried to call right before it had anything in it, it was only declared.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜