简体   繁体   English

将字符串数组从一类传递到另一类

[英]passing array of string from one class to another

I'm trying to pass a string array from one class to another in my java code. 我试图在我的Java代码中将字符串数组从一个类传递到另一个类。 By using code such as 通过使用诸如

Class instanceofclass = new Class();
String text1 = instanceofclass.variablename;

and then displaying it out on screen worked fine! 然后在屏幕上显示出来效果很好!

However I'm trying to get the array of a string. 但是我正在尝试获取字符串数组。 So in my first class I have 所以在我的第一堂课上

JButton [] FilmTime = new JButton[5];                       
JButton [] FilmNames = new JButton[8];
String [] films = new String [8];
DbConnector dataBase = new DbConnector();
for (int i =0; i <= 7; i++)
        {
            films[i] = dataBase.FilmTitle[i];
        }
    for (int i =0; i<= 7; i++)
        {
            FilmNames[i] = new JButton (films[i] + " ("+age+")");
            FilmNames[i].setPreferredSize(new Dimension(563,50));

            grid.add(FilmNames[i]);
        }

I know I can use one for loop but I was just checking issues at the momment. 我知道我可以使用for循环,但是我只是在检查问题。

In my second class used named dbconnector i have: 在第二个使用名为dbconnector的类中,我有:

public String FilmTitle [];
    for (int i =0; i<=7; i++)
                {
                    String query = "SELECT * FROM films WHERE ID =" + i;
                    Rs = St.executeQuery(query);
                        while (Rs.next())
                        {   
                            FilmTitle[i] = Rs.getString("FilmName");

                        }
                }

Eclipse gives the error as: Eclipse给出错误为:

Error is: java.lang.NullPointerException
java.lang.NullPointerException
    at main.init(main.java:46)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Line 46 is the statement in the first for loop... testingme[i] = instanceofclass.variablename[i]; 第46行是第一个for循环中的语句... testingme [i] = instanceofclass.variablename [i];

您必须初始化字符串数组。

String[] testingme = new String[8];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM