Solved

Expected {  Difficulty with variables scope

Posted on 2004-08-14
13
389 Views
Last Modified: 2008-01-16
I am trying to compile and get Expected { at line    public class nextpage() {

Could you look at my code and let me know how to fix it?  I have created the nextpage class to open a database and load url paths into an array.  From there I want to use those variables in the  nextpagebutton_action.  The way the code is now has only two errors left, and I don't understand enough to get over the hump.  Please help.


package blackmeatpit2;

import javax.faces.*;
import com.sun.jsfcl.app.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.faces.component.html.*;

public class Page1 extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Definition">

    private int __placeholder;

    private HtmlForm form1 = new HtmlForm();

    public HtmlForm getForm1() {
        return form1;
    }

    public void setForm1(HtmlForm hf) {
        this.form1 = hf;
    }

    private HtmlGraphicImage image1 = new HtmlGraphicImage();

    public HtmlGraphicImage getImage1() {
        return image1;
    }

    public void setImage1(HtmlGraphicImage hgi) {
        this.image1 = hgi;
    }

    private HtmlGraphicImage image2 = new HtmlGraphicImage();

    public HtmlGraphicImage getImage2() {
        return image2;
    }

    public void setImage2(HtmlGraphicImage hgi) {
        this.image2 = hgi;
    }

    private HtmlCommandButton nextpagebutton = new HtmlCommandButton();

    public HtmlCommandButton getNextpagebutton() {
        return nextpagebutton;
    }

    public void setNextpagebutton(HtmlCommandButton hcb) {
        this.nextpagebutton = hcb;
    }
    // </editor-fold>
    public Page1() {
        // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Initialization">
        try {
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }
        // </editor-fold>
        // Additional user provided initialization code
    }

    protected blackmeatpit2.ApplicationBean1 getApplicationBean1() {
        return (blackmeatpit2.ApplicationBean1)getBean("ApplicationBean1");
    }

    protected blackmeatpit2.SessionBean1 getSessionBean1() {
        return (blackmeatpit2.SessionBean1)getBean("SessionBean1");
    }

    /**
     * Bean cleanup.
     */
    protected void afterRenderResponse() {
    }

    public class nextpage() {
        String[] thumblocation = new String[100];
        String[] sourcelocation = new String[100];
        int placeholder = 5;
        int i;
       
        //Load SQL Server driver
        java.lang.Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
       
        //Create the Connection and Statement
        String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=meatpit1";
        Connection Conn = java.sql.DriverManager.getConnection(url, "sqlserver", "sqlserver");
               
        //Run query
        String sql = "select * from meatpit where site = 'black'";
        Statement stmt = Conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
       
        //Load Results into Array
        while ( rs.next()) {
            for (i = 0; i < 100; i++) {
                thumblocation[i] = rs.getString(3);
                sourcelocation[i] = rs.getString(2);
            }
        }
            return null;
    }
   
    public String nextpagebutton_action() {
        // User event code here...
        image1.setUrl(thumblocation[placeholder +1]);
        image1.setOnclick(thumblocation[placeholder+1]);
        return null;
    }
}
0
Comment
Question by:cxxxcxxx
  • 5
  • 3
  • 2
  • +1
13 Comments
 
LVL 5

Expert Comment

by:TrekkyLeaper
ID: 11802937
"class" is not a valid return type. If you're looking for a generic object, use Object. "class" implies that you're declaring an inner class.
0
 
LVL 5

Expert Comment

by:TrekkyLeaper
ID: 11802941
Or, since you're always returning null, use void:

public void nextpage() {

and remove the return statement.
0
 
LVL 4

Assisted Solution

by:aratani
aratani earned 250 total points
ID: 11803038
Or return the object that you were planning to return on the class nextpage (). So, if you were planning to return a String call it,

public String nextpage () {

...

Take care

AJ
0
 

Author Comment

by:cxxxcxxx
ID: 11806336
OK, I've made some changes, I've declared my variables public static in the correct place now.  I've moved the code around.  I'm using a public static void main() to get my initial setup (contact database, load array, initialize objects).  Now I'm getting a compile error that "non-static variable image1 cannot be referenced from a static context".  I have two images on the page, and I want to initialize them with the first images.  See code below:

/*
 * Page1.java
 *
 * Created on August 14, 2004, 8:03 PM
 * Copyright Charles
 */
package blackmeatpit2;

import javax.faces.*;
import com.sun.jsfcl.app.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.faces.component.html.*;

public class Page1 extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Definition">

    private HtmlForm form1 = new HtmlForm();

    public HtmlForm getForm1() {
        return form1;
    }

    public void setForm1(HtmlForm hf) {
        this.form1 = hf;
    }

    private HtmlGraphicImage image1 = new HtmlGraphicImage();

    public HtmlGraphicImage getImage1() {
        return image1;
    }

    public void setImage1(HtmlGraphicImage hgi) {
        this.image1 = hgi;
    }

    private HtmlGraphicImage image2 = new HtmlGraphicImage();

    public HtmlGraphicImage getImage2() {
        return image2;
    }

    public void setImage2(HtmlGraphicImage hgi) {
        this.image2 = hgi;
    }

    private HtmlCommandButton nextpagebutton = new HtmlCommandButton();

    public HtmlCommandButton getNextpagebutton() {
        return nextpagebutton;
    }

    public void setNextpagebutton(HtmlCommandButton hcb) {
        this.nextpagebutton = hcb;
    }
    public static int firsttime = 0;
    public static String[] thumblocation = new String[100];
    public static String[] sourcelocation = new String[100];
    public static int placeholder = 0;
    public static int i = 0;
   
   
    public static void main(String args[]) throws ClassNotFoundException, SQLException {
        //Load SQL Server driver
        java.lang.Class.forName("com.mysql.jdbc.Driver");

        //Create the Connection and Statement
        String url = "jdbc:mysql://localhost:3306/meatpit";
        Connection Conn = java.sql.DriverManager.getConnection(url, "", "");

        //Run query
        String sql = "select * from meatpit where site = 'black";
        Statement stmt = Conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
       
        //Load Results into Array
        while ( rs.next()) {

            thumblocation[i] = rs.getString(3);
            sourcelocation[i] = rs.getString(2);
            i = i + 1;
        }
               
        image1.setUrl(thumblocation[0]);
        image1.setOnclick(sourcelocation[0]);
       
        image2.setUrl(thumblocation[1]);
        image2.setOnclick(sourcelocation[1]);
        placeholder = 2;
       
}

    private HtmlCommandButton button1 = new HtmlCommandButton();

    public HtmlCommandButton getButton1() {
        return button1;
    }

    public void setButton1(HtmlCommandButton hcb) {
        this.button1 = hcb;
    }

    private HtmlCommandButton indexbutton = new HtmlCommandButton();

    public HtmlCommandButton getIndexbutton() {
        return indexbutton;
    }

    public void setIndexbutton(HtmlCommandButton hcb) {
        this.indexbutton = hcb;
    }
    // </editor-fold>
    public Page1() {
        // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Initialization">
        try {
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof javax.faces.FacesException ? (FacesException) e : new FacesException(e);
        }
        // </editor-fold>
        // Additional user provided initialization code

    }

    protected blackmeatpit2.ApplicationBean1 getApplicationBean1() {
        return (blackmeatpit2.ApplicationBean1)getBean("ApplicationBean1");
    }

    protected blackmeatpit2.SessionBean1 getSessionBean1() {
        return (blackmeatpit2.SessionBean1)getBean("SessionBean1");
    }

    /**
     * Bean cleanup.
     */
    protected void afterRenderResponse() {
    }

    public String nextpagebutton_action() {
        // User event code here...
        if (placeholder < (i - 2)) {
            image1.setUrl(thumblocation[placeholder +1]);
            image1.setOnclick("window.location = " +sourcelocation[placeholder+1]);

            image2.setUrl(thumblocation[placeholder +2]);
            image2.setOnclick("window.location = " +sourcelocation[placeholder+2]);

            placeholder = placeholder + 2;
        }
        if (placeholder >= i + 2) nextpagebutton.setDisabled(true);
        if (placeholder > 2) button1.setDisabled(false);
        return null;
   
    }

    public String button1_action() {  // Previous button
        // User event code here...
        if (placeholder > 0) {
            image1.setUrl(thumblocation[placeholder - 2]);
            image1.setOnclick("window.location = " +sourcelocation[placeholder -2]);

            image1.setUrl(thumblocation[placeholder - 1]);
            image1.setOnclick("window.location = " +sourcelocation[placeholder -1]);
            placeholder = placeholder - 2;
        }
        if (placeholder < i) nextpagebutton.setDisabled(false);
        if (placeholder < 3) button1.setDisabled(true);
        return null;
    }
}
0
 
LVL 4

Expert Comment

by:aratani
ID: 11806565
Your error is coming because you declared the image1 variable in main. main is a static method which basically means there is only one copy of main no matter how many ever copies of class Page1 you create. Like the variable int firsttime = 0; is a static variable so it has only one copy, and can be accessed from main.

image1 and image2 are private variables within the class and it is non-static, i.e. there will be a different copy of image1 and image2 for every object of class Page1 you create.

There is a rule most Java developers use i.e. declare main in a different class as the running class and decalre objects of the classes you need in the class. For example, declare main in a class called Page1Run.

However, if you want to declare main in the same class you need to declare an object of the class Page1 in the main class and use that object to do the intialization i.e.

public static void main(String args[]) throws ClassNotFoundException, SQLException {

        //Load SQL Server driver
        java.lang.Class.forName("com.mysql.jdbc.Driver");

        //Create the Connection and Statement
        String url = "jdbc:mysql://localhost:3306/meatpit";
        Connection Conn = java.sql.DriverManager.getConnection(url, "", "");

        //Run query
        String sql = "select * from meatpit where site = 'black";
        Statement stmt = Conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
       
        //Load Results into Array
        while ( rs.next()) {

            thumblocation[i] = rs.getString(3);
            sourcelocation[i] = rs.getString(2);
            i = i + 1;
        }

       /******************************************/
       //Declare an object of class Page1
       Page1 page = new Page1 ();
         
        //Use page to set all the vallues for image1 and image2
        page.image1.setUrl(thumblocation[0]);
        page.image1.setOnclick(sourcelocation[0]);
       
        page.image2.setUrl(thumblocation[1]);
        page.image2.setOnclick(sourcelocation[1]);
       
       //Don't need to use page to set the placeholder since it is static (static variables can be accessed from a static method directly
       placeholder = 2;
       
}

Hope it helps

AJ

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:cxxxcxxx
ID: 11806804
OK.  That makes sense, and the program compiles just fine.  But when I run it, none of my initialization tasks are being done in the main() section.  The goal is to have the database read and array loaded before the rest of the program runs.  It's driving me crazy.  Thanks for your help AJ... anymore insight you can give me is greatly appreciated ;-)
0
 
LVL 4

Expert Comment

by:aratani
ID: 11806854
Well, the most I can tell you is that your code looks fine. Now, it is upto to you to debug and figure out whether you are getting connected to the databse or whether the SQL query you give is correct or so on. There can be any error in your code.

The best way I use to figure out an error is to use System.out.println statements to figure out where the error is coming from. For example, I would first make sure you first even get a connection by checking the Conn object. I would print out some property of the Conn object to make sure. You can find out what the Connection class has by looking at the Java API (on http://java.sun.com/).

No program will ever run the first time you compile (unoless you are a genious or totally lucky). But, a program compiling doesn't mean it is perfect. It just means that there are no syntax errors. Now, you have to figure out the runtime errors .. :) .. it is one of the essentials of programming. Best of luck!

Take care

AJ
0
 
LVL 5

Accepted Solution

by:
avya2k earned 250 total points
ID: 11809546
Are you running page1.java directly or calling from some other class

It is better practise (in this case) to create the Constructor fpr your class and make an object of your class anywhere from main or from other class

this should work fine
cause anyway constructor is always being called and through that everything can be initialized
0
 
LVL 4

Expert Comment

by:aratani
ID: 11851428
I believe we answered his question. And what he asked us last, we cannot do until we see his database, and that should probably be another question.

Thanks

AJ

0
 

Author Comment

by:cxxxcxxx
ID: 11864252
I finally figured out the problem on my own.  The code that I wanted to execute when the page is first loaded simply needed to be moved into the public Page1() CONSTRUCTOR... uuuugghhh!  I had the code located in public class Page1 instead of public Page1().  
0
 
LVL 4

Expert Comment

by:aratani
ID: 11864670
Well, we helped you remove all the bugs in your code. We answered your question which was,

"I am trying to compile and get Expected { at line    public class nextpage() {"

And how do fix that? And also, some further questions with compilation errors.

So, I think some of us deserve some points for helping you out and pointing you in the right direction.

AJ
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now