NullPointerException:两个集合和一个 for 循环

发布于 2024-08-26 09:23:46 字数 1976 浏览 8 评论 0原文

在另一种方法 (ALGO_1) 中,我搜索 的元素并检查值 H_NAME 是否等于 main.c 中输入的值。当我尝试运行代码时,出现空指针异常。在尝试在代码片段中的每个 for 循环之后打印(使用 System.out.println 等)H_NAME 值时,我还会收到返回给我的 null 语句。

我相当确定该集合根本不存储扫描仪收集的数据。但是当我使用 size() 检查集合大小时,它的大小大约是正确的。

主要问题是:

  • readBackground方法中的data.add在错误的地方?
  • 该片段的结构是否错误?

当我使用 System.out.println 检查后台对象值名称、开始时间、增量等时,它们打印得很好。

for (Hydro hd: hydros) {        
    System.out.println(hd.H_NAME);  
    for (Background back : backgs) {  
        System.out.println(back.H_NAME);  
        if (back.H_NAME.equals(hydroName)) { //get error here  

            public static Collection<Background> readBackground(String url) throws IOException {

                URL u = new URL(url);
                InputStream is = u.openStream();  
                InputStreamReader isr = new InputStreamReader(is);  
                BufferedReader b = new BufferedReader(isr);  
                String line ="";  
                Vector<Background> data = new Vector<Background>();  
                while ((line = b.readLine())!= null) {  
                    Scanner s = new Scanner(line);  
                    String name = s.next();  
                    double starttime = Double.parseDouble(s.next());  
                    double increment = Double.parseDouble(s.next());  
                    double sum = 0;  
                    double p = 0;  
                    double nterms = 0;  
                    while ((s.hasNextDouble())) {  
                        p = Double.parseDouble(s.next());  
                        nterms++;  
                        sum += p;  
                    }  
                    double pbmean = sum/nterms;  
                    Background SAMP = new Background(name, starttime, increment, pbmean);  
                    data.add(SAMP);  
                }  
                return data;  
            }

In another method (ALGO_1) I search over elements of <background> and check the value H_NAME equals a value entered in the main. When I attempt to run the code, I get a null pointer exception. Upon trying to print (with System.out.println etc) the H_NAME value after each for loop in the snippet, I also get a null statement returned to me.

I am fairly certain that the collection is simply not storing the data gathered up by the Scanner. But when I check the collection size with size(), it is about the right size.

Main questions are:

  • from the readBackground method is the data.add in the wrong place?
  • is the snippet simply structured wrongly?

When I use System.out.println to check the Background object values name, start time, increment, etc., they print out fine.

for (Hydro hd: hydros) {        
    System.out.println(hd.H_NAME);  
    for (Background back : backgs) {  
        System.out.println(back.H_NAME);  
        if (back.H_NAME.equals(hydroName)) { //get error here  

            public static Collection<Background> readBackground(String url) throws IOException {

                URL u = new URL(url);
                InputStream is = u.openStream();  
                InputStreamReader isr = new InputStreamReader(is);  
                BufferedReader b = new BufferedReader(isr);  
                String line ="";  
                Vector<Background> data = new Vector<Background>();  
                while ((line = b.readLine())!= null) {  
                    Scanner s = new Scanner(line);  
                    String name = s.next();  
                    double starttime = Double.parseDouble(s.next());  
                    double increment = Double.parseDouble(s.next());  
                    double sum = 0;  
                    double p = 0;  
                    double nterms = 0;  
                    while ((s.hasNextDouble())) {  
                        p = Double.parseDouble(s.next());  
                        nterms++;  
                        sum += p;  
                    }  
                    double pbmean = sum/nterms;  
                    Background SAMP = new Background(name, starttime, increment, pbmean);  
                    data.add(SAMP);  
                }  
                return data;  
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2024-09-02 09:23:46

如果您在调用 back.H_NAME.equals(xxx) 时收到 NullPointerException (NPE),则可能意味着 back 为 null 或 back.H_NAME 为空。

您说当您打印 back.H_NAME 时,您会得到 null,因此这表明 back.H_NAME 实际上是 null。您还没有向我们展示任何描述 Background 类的代码,因此很难为您提供更多帮助。

If you get a NullPointerException (NPE) when you call back.H_NAME.equals(xxx), it probably means either back is null or back.H_NAME is null.

You say that when you print back.H_NAME, you get null, so that suggests that back.H_NAME is actually null. You haven't shown us any code that describes the class Background, so it's hard to give you any more help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文