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

时间:2016-10-02 20:14:24

标签: java jsp constructor

我正在尝试从txt文件中读取并将其显示到JSP文件中。

Bean类从FileReaderz类获取Beanx。 我在jsp中创建了一个 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()); } } 对象,但这就是我遇到的问题

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>

当我在JSP页面中尝试这个时,

我收到错误:

L

1 个答案:

答案 0 :(得分:1)

这是因为Beanx类构造函数抛出Exception并且您在声明标记中创建了Beanx对象而没有处理异常。

要解决此问题,只需使用null初始化声明标记中的Beanx,如下所示:

<%! Beanx bean =null; %>

无论何时需要在scriptlet标记中使用此bean,请使用以下代码:

<% 
   try{

      bean=new Beanx();

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

这与以下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
      }
 }
}