The Artima Developer Community
Sponsored Link

Java Answers Forum
Unreported exception-- Please help :-)

1 reply on 1 page. Most recent reply: Jan 14, 2003 11:25 PM by Sarma K T R B

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Sabrina

Posts: 1
Nickname: sabrina
Registered: Jan, 2003

Unreported exception-- Please help :-) Posted: Jan 13, 2003 5:46 PM
Reply to this message Reply
Advertisement
I have an unreported exception coming up when I try to compile my servlet. I can't figure out why. Can anyone help me?
The exception says
"unreported exception java.langClassNotFoundException; must be caught or declared to be thrown
votes = (int [] indat.readObject();"

Here's my code:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Survey extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {
int votes[] = null;
int index;
int vote;
File survdat = new File("survdat.dat");
String gender;
String products[] = {"Conventional TX", "HDTV", "VCR",
"CD Player/recorder", "DIVD player", "Other"};

// Set the content type for the response output and get a writer

response.setContentType( "text/html" );
PrintWriter servletOut = response.getWriter();

// Produce the head of the output document

servletOut.print("<html><head>");
servletOut.println(
"<title> Return message </title></head><body>");

// If the file already exists, read in its data

if (survdat.exists()) {
ObjectInputStream indat = new ObjectInputStream(new FileInputStream(survdat));
votes = (int []) indat.readObject();
indat.close();
}

// If file does not exist (this is the first vote), create votes array

else
votes = new int[14];

// Get the gender of the survey respondee

gender = request.getParameter("gender");

// Add the consume electronics vote of the response to the votes array

vote = Integer.parseInt(request.getParameter("vote"));
if (gender.equals("male")) vote += 7;
votes[vote]++;

// Write updates votes array to disk

ObjectOutputStream outdat = new ObjectOutputStream(
new FileOutputStream(survdat));
outdat.writeObject(votes);
outdat.flush();
outdat.close();

// Create the initial response information

servletOut.println("<h3>Thank You...</h3>");
servletOut.println("<h4>Current Survey Results:</h4>");

// Create the ttal votes return information for female respondents

servletOut.println("<h5>For Female Respondents </h5>");
for (index = 0; index < 7; index++) {
servletOut.print(products[index]);
servletOut.print(": ");
servletOut.println(votes[index]);
servletOut.println("<br />");
}

// Create the total votes return information for male respondents

servletOut.println("<h5>For Male Respondents </h5>");
for (index = 0; index < 7; index++) {
servletOut.print(products[index - 7])


Sarma K T R B

Posts: 5
Nickname: sarma2312
Registered: Jan, 2003

Re: Unreported exception-- Please help :-) Posted: Jan 14, 2003 11:25 PM
Reply to this message Reply
just modify your code as below:

try{
if (survdat.exists()) {
ObjectInputStream indat = new ObjectInputStream(new FileInputStream(survdat));
votes = (int []) indat.readObject();
indat.close();
}
// If file does not exist (this is the first vote), create votes array

else
votes = new int[14];
}catch(Exception ex){// code comes here if an exception is raised}

Flat View: This topic has 1 reply on 1 page
Topic: Import statement Previous Topic   Next Topic Topic: harddisk selector

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use