简体   繁体   English

默认构造函数无法处理隐式超级构造函数抛出的异常类型异常

[英]Default constructor cannot handle exception type Exception thrown by implicit super constructor

I am trying to read from a txt file and display it to a JSP file.我正在尝试从 txt 文件中读取并将其显示到 JSP 文件中。

The Bean class gets BufferedReader from the FileReaderz class. Bean 类从FileReaderz类获取BufferedReader I create a Beanx object in jsp, but this is where I get stuck我在 jsp 中创建了一个Beanx对象,但这就是我卡住的地方

 public class Beanx {
        public OuterBlock[] outer=new OuterBlock[100];
        public InnerBlock[] inner=new InnerBlock[100];

        /*public List<String> token1 = new ArrayList<String>();
        public List<String> token2 = new ArrayList<String>();*/
        //String[] token1;
        //String[] token2;

        public void callFileReaderz()throws Exception{
            FileReaderz fr=new FileReaderz();
            BufferedReader br1=FileReaderz.br;
            String[] token;
            int i=0,j=0;

            String line;
            while((line=br1.readLine())!=null){
                //System.out.println(line);
                token=line.split("#");
                //List<String> tokenList=Arrays.asList(token);
                if(token.length==5){
                    OuterBlock outerObj=new OuterBlock(token[0],Integer.parseInt(token[1]),Integer.parseInt(token[2]),
                            Float.parseFloat(token[3]),Float.parseFloat(token[4]));
                    this.outer[i++]=outerObj;
                    //System.out.println(token[0]);

                }
                else{
                    InnerBlock innerObj = new InnerBlock(token[0],token[1],Integer.parseInt(token[2]),
                            Integer.parseInt(token[3]),Float.parseFloat(token[4]),Float.parseFloat(token[5]));
                    this.inner[j++]=innerObj;

                }
            }
            br1.close();

        }

        public Beanx() throws Exception{
            this.callFileReaderz();
        }
    }


    public class FileReaderz {
        public static BufferedReader br;

        public static void main (String[] args)throws Exception {
            // TODO Auto-generated method stub
            //public FileReaderz() throws FileNotFoundException{
            br=new BufferedReader(new FileReader("database1.txt"));

    //System.out.println(br.readLine());
    }

}

When I try this in JSP page,当我在 JSP 页面中尝试这个时,

I get the error:我收到错误:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 15 in the jsp file: /dashBoard.jsp
Default constructor cannot handle exception type Exception thrown by implicit super constructor. Must define an explicit constructor
12: </head>
13: 
14: <body>
15: <%! Beanx bean=new Beanx(); %>
16: <div class="table-title">
17: <h3>Projects In Repository</h3>
18: </div>

This is because the Beanx class constructor throws Exception and you are creating Beanx object in declaration tag without handling the exception.这是因为Beanx类构造函数抛出Exception而您正在声明标记中创建Beanx对象而不处理异常。

To resolve this issue just initialize the Beanx in declaration tag with null as below:要解决此问题,只需将声明标记中的 Beanx 初始化为null ,如下所示:

<%! Beanx bean =null; %>

And whenever you need to use this bean in scriptlet tag use the below code:每当您需要在 scriptlet 标记中使用此 bean 时,请使用以下代码:

<% 
   try{

      bean=new Beanx();

      }catch(Exception e){
      //To do something
      }
 %>

This is same as in below java code:这与下面的java代码相同:

class Main extends HttpServlet{

Beanx bean =null;

 protected void doGet(HttpServletRequest request, HttpServletResponse response){
    try{

      bean=new Beanx();

      }catch(Exception e){
      //To do something
      }
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 默认构造函数无法处理隐式超级构造函数抛出的异常类型 FileNotFoundException。 必须定义一个显式构造函数 - Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor 默认构造函数无法处理隐式超级构造函数引发的异常类型IOException。 必须定义一个显式构造函数 - Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor 默认构造函数无法处理隐式超级构造函数引发的异常类型ioexception - default constructor cannot handle exception type ioexception thrown by implicit super constructor 默认构造函数无法处理由隐式超级构造函数抛出的异常类型SocketException - Default constructor cannot handle exception type SocketException thrown by implicit super constructor 无法处理隐式超级构造函数引发的异常类型 - Cannot handle exception type thrown by implicit super constructor Java错误默认构造函数无法处理隐式超级构造函数引发的异常类型SQLException。 必须定义一个显式构造函数 - Java error Default constructor cannot handle exception type SQLException thrown by implicit super constructor. Must define an explicit constructor 构造默认构造函数时无法处理异常:隐式超级构造函数抛出的类型异常 - While constructing the default constructor can not handle exception : type Exception thrown by implicit super constructor 默认构造函数无法处理引发的异常类型IOException - Default constructor cannot handle exception type IOException thrown 为什么默认构造函数无法处理异常类型Exception? - Why default constructor cannot handle exception type Exception? Java错误:默认构造函数无法处理异常类型FileNotFound Exception - Java error: Default constructor cannot handle exception type FileNotFound Exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM