• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Illegal Start of the "try" Block?!

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program with try, catch, finally blocks in it. I do not understand why the compiler says that
1. UserDirectory.java:34: Illegal start of type try
(line 34 is flagged in the code below)
2. UserDirectory.java:84: <identifier> expected }
(line 84 is also flagged in the code below)
Please help.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The code between line 34 and line 84 is outside of any method or initialization block, so the compiler assumes "try" is supposed to be the return type of a method. You've got methods declared inside the try block, too, which are also illegal; you can only define methods at class scope, and you can only put code (try blocks, for example) inside a method or an initialization block.
I'm really confused about what you're trying to do in a lot of places:
  • Who is supposed to call the isUserLogon() method?
  • At line 32, you initilize a member _username with a call to fixId() passing the member username as an argument. But username isn't initialized in the constructor, so when this code runs, you'll get a NullPointerException.
  • What should initiate the execution of the code in the big try block?

  • I hope I'm not insulting you, but I'm guessing you're not an experienced Java programmer, yes? You might want to start out with an introductory Java book like
    Head First Java before jumping into something as involved as working with Struts (this is derived from something in Struts in Action, yes?)
     
    JiaPei Jen
    Ranch Hand
    Posts: 1309
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, Ernest. You correctly pointed out my problems. I do not have much hands-on experience in IT. Nonetheless, I am learning a lot (in Java, Servlet, JSP, and Struts) by constructing a web site.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic