Solved

getConstructor in JAVA

Posted on 2013-01-15
16
499 Views
Last Modified: 2013-01-15
Dear Experts,
I have a JDialog and want to call the JDialog using getConstructor. I am unable to pass String array to the constructor and system is giving error like
"java.lang.NoSuchMethodException: guestName.<init>(java.lang.Integer, java.lang.String, boolean, java.awt.Frame)
      at java.lang.Class.getConstructor0"
I want to pass Integer,String Array, Boolean and Frame. I do require your help know what I should class should I declare to pass an array of String[][]. The sintex which I used is as under

String guestName2[][] = {{"A","0","B"},{"C","1","D"}}; //,{"E","2","F"},{"G","3","H"}};
int gc = Integer.parseInt(Guest_text.getText());
String retv = "guestName";
try {
Class cl = Class.forName(retv);
Constructor constructor =  cl.getConstructor(new Class[] {Integer.class, String.class, boolean.class, Frame.class});
Object invoker = constructor.newInstance(new Object[]{gc, guestName2, true, (new javax.swing.JFrame())});
}

guestName2 is an two dimensional array.


Regards
0
Comment
Question by:MILIND_JOG
  • 7
  • 6
  • 3
16 Comments
 
LVL 16

Expert Comment

by:Valeri
ID: 38778030
try this:

public class GuestName extends JDialog {
    private Integer yourInt;
    private String[][] yourArray;

    public GuestName(Integer yourInt, String[][] yourArray, Frame owner, boolean modal) throws HeadlessException {
        super(owner, modal);
        this.yourInt = yourInt;
        this.yourArray = yourArray;
    }
}
0
 

Author Comment

by:MILIND_JOG
ID: 38778217
Dear Valery,

Thanks for fast response.

I have two seperate JDialog
1> newGuest and 2> guestName

I want to call guestName from newGuest

guestName constructor is having 4 arguments
          public guestName(int gn,String guestName1[][], boolean modal, java.awt.Frame parent)

From newGest, I am calling guestName as below

String guestName2[][] = {{"A","0","B"},{"C","1","D"}}; //,{"E","2","F"},{"G","3","H"}};
int gc = Integer.parseInt(Guest_text.getText());
String retv = "guestName";
try {
Class cl = Class.forName(retv);
Constructor constructor =  cl.getConstructor(new Class[] {Integer.class, String.class, boolean.class, Frame.class});
Object invoker = constructor.newInstance(new Object[]{gc, guestName2, true, (new javax.swing.JFrame())});
}

I am getting error as
java.lang.NoSuchMethodException: guestName.<init>(java.lang.Integer, [[Ljava.lang.String;, boolean, java.awt.Frame)
      at java.lang.Class.getConstructor0(Class.java:2706)

Please give your inputs.
Regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38778230
I have a JDialog and want to call the JDialog using getConstructor.
Why? Why are you using reflection?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:MILIND_JOG
ID: 38778266
Dear CEHJ,

Please suggest the alternate.

Regards
0
 

Author Comment

by:MILIND_JOG
ID: 38778284
Dear CEHJ,
I want to instanciate the second JDialog after some inputs in first JDialog and values need to pass from one to other.

Regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38778290
Please suggest the alternate.
I can't really, since you don't give your GOAL
Gernerally speaking, reflection should only be used for very specialized purposes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38778299
I want to instanciate the second JDialog after some inputs in first JDialog and values need to pass from one to other.
Then all you need to do is to provide a constructor in your JDialog subclass that accepts the required parameters.
0
 

Author Comment

by:MILIND_JOG
ID: 38778313
Dear CEHJ,
I tried for the same but I am getting error.

java.lang.NoSuchMethodException: guestName.<init>(java.lang.Integer, [[Ljava.lang.String;, boolean, java.awt.Frame)
      at java.lang.Class.getConstructor0(Class.java:2706)

I have passed exectlty the same which is required in second JDialog.

Regards
0
 

Author Comment

by:MILIND_JOG
ID: 38778319
Deear CHEJ,
I am suspecting error due to [[Ljava.lang.String

Regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38778344
What you should be doing is something like this:

MySecondDialog d = new MySecondDialog(someStringArray, true, someFrame);

Open in new window

0
 

Author Comment

by:MILIND_JOG
ID: 38778359
Ok CHEJ,

Let me try and will come back to you.

Regards
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 38778363
(Obviously you will need to create a JDialog subclass to do that)
0
 

Author Closing Comment

by:MILIND_JOG
ID: 38778414
Issue closed and supported well.

Regards.
0
 
LVL 16

Expert Comment

by:Valeri
ID: 38778425
Hei Milind,
CEHJ is right, don't use reflection! Use the classes in this way (I created some for you)

import javax.swing.*;

public class JFrameTester {

      public static void main(String[] args) {
        JFrame f = new JFrame("your JFrame");
        f.setSize(400, 400);
        f.setLocation(300,200);
        f.setVisible(true);

        String guestName2[][] = {{"A","0","B"},{"C","1","D"}}; //,{"E","2","F"},{"G","3","H"}};

        NewGuest newGuest = new NewGuest(f, false, 5, guestName2);
        newGuest.setSize(250, 250);
        newGuest.setVisible(true);

        newGuest.showGuestNname();  

      }
}

=============================================================

import javax.swing.*;
import java.awt.*;

public class NewGuest extends JDialog {
    private Integer yourInt;
    private String[][] guestName2;
    private Frame owner;

    public NewGuest(Frame owner, boolean modal, Integer yourInt, String[][] guestName2) throws HeadlessException {
        super(owner, modal);
        this.owner = owner;
        this.yourInt = yourInt;
        this.guestName2 = guestName2;
    }

    public void showGuestNname() {
        GuestName guestName = new GuestName(owner, false, 5, guestName2);
        guestName.setSize(150, 150);
        guestName.setVisible(true);
    }
}

=======================================================

import javax.swing.*;
import java.awt.*;

public class GuestName extends JDialog {
    private Integer yourInt;
    private String[][] guestName2;

    public GuestName(Frame owner, boolean modal, Integer yourInt, String[][] guestName2) throws HeadlessException {
        super(owner, modal);
        this.yourInt = yourInt;
        this.guestName2 = guestName2;
    }
}

then start the first class! forget about reflection, it's for other purposes.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38778436
:)
0
 
LVL 16

Expert Comment

by:Valeri
ID: 38778456
CEHJ was faster than me ;-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 56
table example 4 32
MySQL  on Tomcat 8 68
learn programming 8 33
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

809 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