DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Big errors i got plz help me out illegal type of expression type, (http://www.daniweb.com/forums/thread201074.html)

karamjeet2004 Jul 1st, 2009 10:08 pm
Big errors i got plz help me out illegal type of expression type,
 
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.sql.*;
public class panel extends JFrame implements ActionListener  {

                ResultSet res;
        Connection conn;
        Statement stat;
       

String lang;
JFrame frame = new JFrame("Graphics Button");
lang="abc";
JButton b1;
JLabel l1,l2,l3;
JPanel p,p1,p2,p3,p4;
p.setSize(100,100);
p.setLayout(new GridLayout(3,0));

p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
l1=new JLabel(lang);
l2=new JLabel();
l3=new JLabel();
ImageIcon button=new ImageIcon("magnifier1.jpg");
b1=new JButton();
b1.setIcon(button);
b1.setOpaque(false);
b1.setFocusPainted(false);
b1.setBorderPainted(false);
b1.setContentAreaFilled(false);
b1.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
b1.setBorderPainted(false);
b1.addActionListener(this);
b1.setBackground(Color.green);
//b2=new JButton("cancel");
p1.setBackground(Color.black);
p1.add(l1);
p2.setBackground(Color.green);
p3.setBackground(Color.red);
p4.setBackground(Color.blue);
p.add(p1);
p.add(p2);
p.add(p3);
p4.add(b1);
//p4.add(b2);
frame.add(p,BorderLayout.CENTER);
frame.add(p4,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:guru","","");
stat = conn.createStatement();
        res = stat.executeQuery("Select * from hotel");
res.next();

//con.close();
}
catch(Exception e)
{
e.printStackTrace();
}//end of try

public void actionPerformed(ActionEvent e)  {
        if(e.getSource() == b1)  {
        try  {
        res.next();
        }
        catch(Exception ee) {}//end of try
        showRecord(res);
        }//end if
        }//end action performed
public void showRecord(ResultSet res)  {
        try  {
        l1.setText(res.getString(1));
        l2.setText(res.getString(2));
        }
        catch(Exception e)  {}//end try
       
        }//end of show record
public static void main(String[] args) {

 panel p=new panel();

p.showRecord(res);
}       
}//end ma
/* errors:-
C:\>javac panel.java
panel.java:14: <identifier> expected
lang="abc";
^
panel.java:18: <identifier> expected
p.setSize(100,100);
^
panel.java:18: illegal start of type
p.setSize(100,100);
^
panel.java:19: <identifier> expected
p.setLayout(new GridLayout(3,0));
^
panel.java:19: illegal start of type
p.setLayout(new GridLayout(3,0));
^
panel.java:21: <identifier> expected
p1=new JPanel();
^
panel.java:22: <identifier> expected
p2=new JPanel();
^
panel.java:23: <identifier> expected
p3=new JPanel();
^
panel.java:24: <identifier> expected
p4=new JPanel();
^
panel.java:25: <identifier> expected
l1=new JLabel(lang);
^
panel.java:26: <identifier> expected
l2=new JLabel();
^
panel.java:27: <identifier> expected
l3=new JLabel();
^
panel.java:29: <identifier> expected
b1=new JButton();
^
panel.java:30: <identifier> expected
b1.setIcon(button);
^
panel.java:30: <identifier> expected
b1.setIcon(button);
^
panel.java:31: <identifier> expected
b1.setOpaque(false);
^
panel.java:31: illegal start of type
b1.setOpaque(false);
^
panel.java:32: <identifier> expected
b1.setFocusPainted(false);
^
panel.java:32: illegal start of type
b1.setFocusPainted(false);
^
panel.java:33: <identifier> expected
b1.setBorderPainted(false);
^
panel.java:33: illegal start of type
b1.setBorderPainted(false);
^
panel.java:34: <identifier> expected
b1.setContentAreaFilled(false);
^
panel.java:34: illegal start of type
b1.setContentAreaFilled(false);
^
panel.java:35: <identifier> expected
b1.setBorder(BorderFactory.createEmptyBord
^
panel.java:35: <identifier> expected
b1.setBorder(BorderFactory.createEmptyBord

panel.java:36: <identifier> expected
b1.setBorderPainted(false);
^
panel.java:36: illegal start of type
b1.setBorderPainted(false);
^
panel.java:37: <identifier> expected
b1.addActionListener(this);
^
panel.java:37: illegal start of type
b1.addActionListener(this);
^
panel.java:38: <identifier> expected
b1.setBackground(Color.green);
^
panel.java:38: <identifier> expected
b1.setBackground(Color.green);
^
panel.java:40: <identifier> expected
p1.setBackground(Color.black);
^
panel.java:40: <identifier> expected
p1.setBackground(Color.black);
^
panel.java:41: <identifier> expected
p1.add(l1);
^
panel.java:41: <identifier> expected
p1.add(l1);
^
panel.java:42: <identifier> expected
p2.setBackground(Color.green);
^
panel.java:42: <identifier> expected
p2.setBackground(Color.green);
^
panel.java:43: <identifier> expected
p3.setBackground(Color.red);
^
panel.java:43: <identifier> expected
p3.setBackground(Color.red);
^
panel.java:44: <identifier> expected
p4.setBackground(Color.blue);
^
panel.java:44: <identifier> expected
p4.setBackground(Color.blue);
^
panel.java:45: <identifier> expected
p.add(p1);
^
panel.java:45: <identifier> expected
p.add(p1);
^
panel.java:46: <identifier> expected
p.add(p2);
^
panel.java:46: <identifier> expected
p.add(p2);
^
panel.java:47: <identifier> expected
p.add(p3);
^
panel.java:47: <identifier> expected
p.add(p3);
^
panel.java:48: <identifier> expected
p4.add(b1);
^
panel.java:48: <identifier> expected
p4.add(b1);
^
panel.java:50: <identifier> expected
frame.add(p,BorderLayout.CENTER);
^
panel.java:50: <identifier> expected
frame.add(p,BorderLayout.CENTER);
^
panel.java:50: <identifier> expected
frame.add(p,BorderLayout.CENTER);
^
panel.java:51: <identifier> expected
frame.add(p4,BorderLayout.SOUTH);
^
panel.java:51: <identifier> expected
frame.add(p4,BorderLayout.SOUTH);
^
panel.java:51: <identifier> expected
frame.add(p4,BorderLayout.SOUTH);
^
panel.java:52: <identifier> expected
frame.pack();
^
panel.java:53: <identifier> expected
frame.setVisible(true);
^
panel.java:53: illegal start of type
frame.setVisible(true);
^
panel.java:55: illegal start of type
try
^
panel.java:65: illegal start of type
catch(Exception e)
^
panel.java:65: ';' expected
catch(Exception e)
^
61 errors
*/

BestJewSinceJC Jul 1st, 2009 10:28 pm
Re: Big errors i got plz help me out illegal type of expression type,
 
Yes. Because you have no main method - you need to put any code (other than variable declarations) inside methods. The main method is the method that runs when you first start your program, *edit*


Oops, nevermind - I see that you do have a main method. For statements like String lang, then lang = "abc", you would need to initially just say String lang = "abc"; . . and what I said about only having variable declarations still stands. lang = "abc" is an assignment statement, and thus, isn't allowed unless you do it inside of a method.

BestJewSinceJC Jul 1st, 2009 10:30 pm
Re: Big errors i got plz help me out illegal type of expression type,
 
edit, sorry guys. Combined the two posts.

karamjeet2004 Jul 2nd, 2009 2:43 am
Re: Big errors i got plz help me out illegal type of expression type,
 
I am not satisfied with this answer

karamjeet2004 Jul 2nd, 2009 2:44 am
Re: Big errors i got plz help me out illegal type of expression type,
 
It gives me errors like previous one

JamesCherrill Jul 2nd, 2009 4:16 am
Re: Big errors i got plz help me out illegal type of expression type,
 
Satisfied or not, he's right. Everything except variable declarations have to be inside a method. Post your latest code, with code=java tags.

karamjeet2004 Jul 2nd, 2009 8:39 am
Re: Big errors i got plz help me out illegal type of expression type,
 
Guys i changed the code now i get 3 errors plz HLP me out errors are after the code
//code
/*<applet code=panel.class height=300 width=500></applet>*/
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.sql.*;
import java.awt.event.*;
import java.net.*;
public class panel extends JApplet implements ActionListener{
        ResultSet res;
        Connection con;
        Statement stat;
        JFrame frame;
        JButton b1;
        JLabel l1,l2,l3;
        JPanel p,p1,p2,p3,p4;
        ImageIcon button;
public void init(){
       
        p=new JPanel();
        p1=new JPanel();
        p2=new JPanel();
        p3=new JPanel();
        p4=new JPanel();
        l1=new JLabel();
        l2=new JLabel();
          l3=new JLabel();
        b1=new JButton();
        frame = new JFrame("Graphics Button");
        button=new ImageIcon("magnifier1.jpg");
        p.setSize(100,100);
        p.setLayout(new GridLayout(3,0));
        p1.setBackground(Color.black);
        p1.add(l1);
        p2.setBackground(Color.green);
        p3.setBackground(Color.red);
        p4.setBackground(Color.blue);
        p.add(p1);
        p.add(p2);
        p.add(p3);
        p4.add(b1);
        b1.setIcon(button);
        b1.setOpaque(false);
        b1.setFocusPainted(false);
        b1.setBorderPainted(false);
        b1.setContentAreaFilled(false);
        b1.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        b1.setBorderPainted(false);
        b1.addActionListener(this);
        frame.add(p,BorderLayout.CENTER);
        frame.add(p4,BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
 }//end init
try
 {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con=DriverManager.getConnection("jdbc:odbc:guru","","");
 try{

        stat = conn.createStatement();
        res = stat.executeQuery("Select * from granth");
        res.next();

//con.close();
    }
catch(Exception e){
        e.printStackTrace();
    }
 }
catch(Exception e){
        e.printStackTrace();
 }//end of try
public void actionPerformed(ActionEvent e)  {
        if(e.getSource() == b1)  {
        try  {
        res.next();
        }
        catch(Exception ee) {}//end of try
        showRecord(res);
        }//end if
        }//end action performed
public void showRecord(ResultSet res)  {
        try  {
        l1.setText(res.getString(1));
        l2.setText(res.getString(2));
        }
        catch(Exception e)  {}//end try
       
        }//end of show record
}
/*errors
C:\Documents and Settings\Desktop>javac panel.java
panel.java:54: illegal start of type
try
^
panel.java:70: illegal start of type
catch(Exception e){
^
panel.java:70: ';' expected
catch(Exception e){
^
3 errors
*/

JamesCherrill Jul 2nd, 2009 8:45 am
Re: Big errors i got plz help me out illegal type of expression type,
 
Everything except variable declarations still have to be inside a method. Indent your code so you can see where methods and classes end. Your errors start immediately after the end of the init method because the following code isn't in a method.
This would have been obvious to you with proper indentation and code tags.

JamesCherrill Jul 2nd, 2009 9:05 am
Re: Big errors i got plz help me out illegal type of expression type,
 
ps: If you have code that looks like this:
catch(Exception ee) {}
then what you are saying is: "if my code goes wrong I do not want to know why or where it happened. Please just stop executing and give me no help at all".
Try replacing all those with
catch(Exception ee) {
  ee.printStackTrace();
}
so you know what's happening.

karamjeet2004 Jul 2nd, 2009 11:32 am
Re: Big errors i got plz help me out illegal type of expression type,
 
thnks my code is now running


All times are GMT -4. The time now is 1:22 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC