hi. I get a NillpointerException.
Here's my code:

private void Login_ButtonMouseClicked(java.awt.event.MouseEvent evt) {                                          
String usrname = uname_TextField.getText();
        String pass = passwd_PasswordField.getText();
        String pwrd = pass.toString();
        String args[] = null;
        boolean auth_flag = false;
        
        try{
            R =  S.executeQuery("select uname, passwrd from Employee_Master;");
            
            while(R.next()){
                if(usrname.equals(R.getString("uname"))){
                    if(pwrd.equals(R.getString("passwrd"))){
           
                        this.dispose();
                     [B]args[0] = R.getString("uname")[/B]//causing nullpointerexception
                        Modules_Frame.main(args);
                        .
                        .
public class Modules_Frame extends javax.swing.JFrame {
.
.
public static void main(final String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Modules_Frame(args).setVisible(true);
            }
        });
    }

Wats the problem?
Thanks.

It might help if you pasted us the entire error message, which would probably include the line number at which this happened, and perhaps other relevant information.

I m trying to pass args[] to a main() function...

java.lang.NullPointerException
        at starterdialogspkg.AppStarter_Dialog.Login_ButtonMouseClicked(AppStarter_Dialog.java:148)
        at starterdialogspkg.AppStarter_Dialog.access$100(AppStarter_Dialog.java:22)
        at starterdialogspkg.AppStarter_Dialog$2.mouseClicked(AppStarter_Dialog.java:66)
        at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
        at java.awt.Component.processMouseEvent(Component.java:6219)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5981)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4583)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4229)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2475)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)
        at java.awt.Dialog$1.run(Dialog.java:1051)
        at java.awt.Dialog$3.run(Dialog.java:1103)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.awt.Dialog.show(Dialog.java:1101)
        at java.awt.Component.show(Component.java:1516)
        at java.awt.Component.setVisible(Component.java:1468)
        at java.awt.Window.setVisible(Window.java:841)
        at java.awt.Dialog.setVisible(Dialog.java:991)
        at starterdialogspkg.AppStarter_Dialog$3.run(AppStarter_Dialog.java:181)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Line 148 is

args[0] = R.getString("uname");

This is declaration of args:

String[] args = null;

You declared the array incorrectly. String[] args declares an array of Strings called args that has no memory allocated to it. So basically, you named the array, but did not give it a size. You can change String[] args = null to String[] args = new String[1]; and then use args[0] = R.getString("uname"); and it should work.

OK. I redecalred it and it works. But the main() method does not get args...
From the main() I want to pass args to the constructor...
Plz help.

If you call the main method with Classname.main(args) it will work. Are you still getting any errors?

Well, I m doing the same thing.

args[0] = R.getString("uname");
 Modules_Frame.main(args);

Now this is the main():

public static void main([B]final String args[][/B]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Modules_Frame([B]args[0][/B]).setVisible(true);
        }
        }
        );
    }

This is the constructor:

public Modules_Frame(String args) {
        System.out.println("ARGS: "+args);
        MySQLConnector dbc = new MySQLConnector();
        S = dbc.connectdb();
        try{

            R = S.executeQuery("select Sales_RT, PP_RT, Reports_RT, Admin_RT from Employee_Master "+
                               "where uname = '"+args+"';");
            while(R.next()){
                if(R.getString("Sales_RT").equals("0"))Sales_Menu.setVisible(false);//setEnabled(false);
                if(R.getString("PP_RT").equals("0"))PP_Menu.setVisible(false);//setEnabled(false);
                if(R.getString("Reports_RT").equals("0"))Reports_Menu.setVisible(false);//setEnabled(false);
                if(R.getString("Admin_RT").equals("0"))Admin_Menu.setVisible(false);//setEnabled(false);
            }
        }catch(SQLException e){System.out.println(e);}
        initComponents();//This will initialize frame components...
        
    }

args can now take the correct value passed to the constructor, but the menus aren't getting disabld .
I chkd the database values. Everytime I run the program, all menus are enabled even if the user dont have the rights.
If the right i.e Sales_RT is 0, the Sales menu should be disabld and so on...
Thanks...

So you're saying that "args" is now being passed correctly, but that the database isn't working as you expected? In that case, I can't help you any further. . you have an entirely different problem now.

hi.
I decided to pass args directly to the constructor instead of main().
It works .
Thanks

Yeah, no problem. If you don't have any more problems you can set this to solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.