Java NullPointerException(Java NullPointerException)
我正在编写两个类来处理简单的拍卖。 我有一个准备好工作的班级,处理单次拍卖的操作,现在我正在编写另一个类似拍卖行的班级,以跟踪所有可用的拍卖。 在测试课程的以下部分时:
import java.util.ArrayList; public class AuctionHouse { private ArrayList<DutchAuction> huutokaupat; public AuctionHouse() { } public void addAuction(DutchAuction newAuction) { huutokaupat.add(newAuction); } }
在一个带有以下代码的main方法中(“kauppa”是一个经过测试和工作的对象变量):
AuctionHouse talo = new AuctionHouse(); talo.addAuction(kauppa);
我明白了:
在ope.auction.dutch.DutchAuctionTest.main(DutchAuctionTest.java:54)的ope.auction.dutch.AuctionHouse.addAuction(AuctionHouse.java:13)中的线程“main”java.lang.NullPointerException中的异常
我该如何解决这个问题?
I'm writing two classes to handle simple auctions. I have a class ready and working, which handles the operations for a single auction, and now I'm writing another class like an auction house, to keep track of all the auctions available. When testing the following part of the class:
import java.util.ArrayList; public class AuctionHouse { private ArrayList<DutchAuction> huutokaupat; public AuctionHouse() { } public void addAuction(DutchAuction newAuction) { huutokaupat.add(newAuction); } }
inside a main method with following code("kauppa" is a tested and working object-variable):
AuctionHouse talo = new AuctionHouse(); talo.addAuction(kauppa);
I get:
Exception in thread "main" java.lang.NullPointerException at ope.auction.dutch.AuctionHouse.addAuction(AuctionHouse.java:13) at ope.auction.dutch.DutchAuctionTest.main(DutchAuctionTest.java:54)
How can I fix the problem?
原文:https://stackoverflow.com/questions/1582215
最满意答案
您可以使用
set()
删除字符串中的重复项,并且只遍历唯一字符串:s=input("Enter your string: ") s=s.split() for i in set(s): print(i, "=", s.count(i)
或者你可以使用
collections.Counter()
:from collections import Counter s=input("Enter your string: ") s=s.split() for key, value in Counter(s).items(): print(key, "=", value)
You can get rid of duplicates in a string by using
set()
and only iterate through unique strings:s=input("Enter your string: ") s=s.split() for i in set(s): print(i, "=", s.count(i)
Alternatively you can use
collections.Counter()
:from collections import Counter s=input("Enter your string: ") s=s.split() for key, value in Counter(s).items(): print(key, "=", value)
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
使用列表理解,可以实现以下目的: vowels = 'aeiou' def vowel_count(txt): counts = map(txt.count, vowels) return ", ".join(vowels) + " appear, respectively, " + ",".join(map(str, counts)) + " times" Using list comprehension, the following can be achieved: vowels = ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
您可以通过使用收集模块中的Counter容器来实现此目的。 从Python文档:“Counter是一个dict子类,用于计算可散列对象。它是一个无序集合,其中元素存储为字典键,它们的计数存储为字典值。计数允许为任何整数值,包括零或负数Counter类与其他语言的bag或multisets类似。“ 这是一个示例代码,可以满足您的要求。 我使用了一个事实,即文件是创建Counter对象的迭代器。 当你迭代时,在一个文件上产生每一行,但不删除换行符,所以我使用strip()方法来获得你建议的输出。 filenam ...
-
冥想: 'b c c d'.split # => ["b", "c", "c", "d"] 'b c c d'.split.group_by{ |w| w } # => {"b"=>["b"], "c"=>["c", "c"], "d"=>["d"]} 'b c c d'.split.group_by{ |w| w }.map{ |k, v| [k, v.count] } # => [["b", 1], ["c", 2], ["d", 1]] 'b c c d'.split.group_by{ |w| w ...
-
您可以使用set()删除字符串中的重复项,并且只遍历唯一字符串: s=input("Enter your string: ") s=s.split() for i in set(s): print(i, "=", s.count(i) 或者你可以使用collections.Counter() : from collections import Counter s=input("Enter your string: ") s=s.split() for key, value in Counte ...
-
如何在python中创建一个打印大写单词的函数(how to create a function in python that prints out uppercase words)[2023-04-14]
你非常接近。 for word in s.split(): if word[0].isupper() 你不是遍历单词,而是字母。 首先split以获得期望的结果。 您还可以从一个函数中将这个修改成一个列表理解 : [word for word in s.split() if word[0].isupper()] 如果您想要做的不仅仅是立即打印出来,这很方便。 You're pretty close. for word in s.split(): if word[0].isupper() ... -
replace将替换char的所有出现。 您需要按照已经看过的字符进行操作,如果重复这些字符则替换JUST这个字符(在特定索引处)。 字符串不支持索引赋值,因此我们可以构建一个表示新字符串的新列表和之后''.join() 。 使用Set您可以按照已经看过的项目进行操作。 它看起来像这样: s = 'Google' seen = set() new_string = [] for c in s: if c.lower() in seen: new_string.append('*') ...
-
创建一组要忽略的单词,并使用if查看单词是否在您的忽略集中: ignore = {"a","of","the","an"} from collections import Counter cnt = Counter(word for line in text.lower() for word in line.split() if word not in ignore) 您可能还想删除单词中的标点符号: from collections import Counter from ...
-
如果要删除少于3个字符的单词,可以执行以下操作: text_more_than_3_char_words = [w for w in text_with_out_special_signs if len(w) > 2] counts_of_words = Counter(text_more_than_3_char_words) # transform list in Counter If you want to remove words with less than 3 characters you c ...