Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对象变量可能尚未初始化_Java_Class Design - Fatal编程技术网

Java 对象变量可能尚未初始化

Java 对象变量可能尚未初始化,java,class-design,Java,Class Design,我找不到在右侧生成的错误。GetText();接近底部。错误是上面的标题。从我所读到的内容来看,成员变量必须用Java初始化,所以我通读了一遍,看了所有这些都没有用。我认为我在默认构造函数中做的一切都是正确的,但我可能错了,这是我的第一个java程序。谢谢你的帮助,我只是不想在这上面再花一个小时 import java.io.*; import java.lang.*; //class Shapes //{ class Triangle { private

我找不到在右侧生成的错误。GetText();接近底部。错误是上面的标题。从我所读到的内容来看,成员变量必须用Java初始化,所以我通读了一遍,看了所有这些都没有用。我认为我在默认构造函数中做的一切都是正确的,但我可能错了,这是我的第一个java程序。谢谢你的帮助,我只是不想在这上面再花一个小时

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();
        }
    }
//}
import java.io.*;
导入java.lang.*;
//类形状
//{ 
阶级三角
{
私有int rowNum;
私有字符串文本;
三角形(){
rowNum=0;
text=“”;
}
public-void-GetText()
{
int标志=0;
字符串trialText=“”;
while(标志==0){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“\n请输入一个单词显示在三角形中(如bob、tom等)”;
试试{
trialText=reader.readLine();
System.out.println(“您输入的”+trialText);//打印用户输入的数据
}
catch(IOException ioe){//在发生输入/输出异常时要执行的语句
System.out.println(“发生意外错误”);
继续;
}
char[]newText=trialText.toCharArray();

对于(int i=0;i在ur main fn中尝试此方法:

Triangle right = new Triangle();
您必须初始化
三角形

尝试以下操作:

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

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

你试图在它有任何东西之前打电话给
right
,它只是被声明了。

lol你的问题不在
C++
试试我的答案,然后再给我回电话(是/否/有时——取决于情况)这太丢脸了,我知道LOL。在堆栈中,你应该选择一个答案作为
已接受
,更多的人会希望在你未来的问题上提供帮助^ ^@ChadM
Triangle right;right.getText();
在Java中就像
Triangle*right;right->getText()在C++中,与唯一的区别是java中未初始化的变量会导致编译时错误。