Crash at tableview.reloaddata with error Unexpectedly found nil












1















On the click of a create button, I navigate to tableviewcontroller screen. But nothing is populated in the tableview since the array which populates the tableview hasn't been called yet.



Now once the tableview screen is reached, after a few seconds another method is called elsewhere(in another file), which in turn calls a function in this tableviewcontroller screen. This is that method of the tableviewcontroller screen which is called...



  func stopIndicator(thegrpName: String) {
stopIndicator()

let realm = try! Realm()
let chatGrp = realm.objects(ChatGroup.self)
chatGroup = chatGrp

tableview.reloadData() //CRASH HAPPENS HERE

}


In this method, once I reach tableview.reloadData() it crashes with the error Unexpectedly found nil while unwrapping an optional value..



I referred this link which seems to have a similar problem...but couldn't get much help out of it...



What could be the reason for this crash...?



EDIT 1: The numberOfRows and cellForRowAt.. is given like so...



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if let chatGrp = chatGroup {
return chatGrp.count
}
return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ChatGroupTableViewCell = tableView.dequeueReusableCell(withIdentifier: "chatgroupIdentifier") as! ChatGroupTableViewCell

let groupChatObj = chatGroup![indexPath.row]
cell.chatLabel.text = groupChatObj.lastMessage?.text?.text
return cell
}









share|improve this question

























  • Please show your numberOfRowsInSection and cellForRowAt methods. Also see this for general troubleshooting and advice for this exception

    – Paulw11
    Nov 23 '18 at 5:39













  • Seems tableview is nil, check the outlet for connection if it's added in a storyboard.

    – Satish
    Nov 23 '18 at 5:43













  • checked that @Satish..its connected properly..

    – user308123
    Nov 23 '18 at 5:44






  • 2





    Set an exception breakpoint to see which line is crashing.

    – Paulw11
    Nov 23 '18 at 5:57






  • 1





    Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.

    – Daniel Springer
    Nov 23 '18 at 6:08
















1















On the click of a create button, I navigate to tableviewcontroller screen. But nothing is populated in the tableview since the array which populates the tableview hasn't been called yet.



Now once the tableview screen is reached, after a few seconds another method is called elsewhere(in another file), which in turn calls a function in this tableviewcontroller screen. This is that method of the tableviewcontroller screen which is called...



  func stopIndicator(thegrpName: String) {
stopIndicator()

let realm = try! Realm()
let chatGrp = realm.objects(ChatGroup.self)
chatGroup = chatGrp

tableview.reloadData() //CRASH HAPPENS HERE

}


In this method, once I reach tableview.reloadData() it crashes with the error Unexpectedly found nil while unwrapping an optional value..



I referred this link which seems to have a similar problem...but couldn't get much help out of it...



What could be the reason for this crash...?



EDIT 1: The numberOfRows and cellForRowAt.. is given like so...



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if let chatGrp = chatGroup {
return chatGrp.count
}
return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ChatGroupTableViewCell = tableView.dequeueReusableCell(withIdentifier: "chatgroupIdentifier") as! ChatGroupTableViewCell

let groupChatObj = chatGroup![indexPath.row]
cell.chatLabel.text = groupChatObj.lastMessage?.text?.text
return cell
}









share|improve this question

























  • Please show your numberOfRowsInSection and cellForRowAt methods. Also see this for general troubleshooting and advice for this exception

    – Paulw11
    Nov 23 '18 at 5:39













  • Seems tableview is nil, check the outlet for connection if it's added in a storyboard.

    – Satish
    Nov 23 '18 at 5:43













  • checked that @Satish..its connected properly..

    – user308123
    Nov 23 '18 at 5:44






  • 2





    Set an exception breakpoint to see which line is crashing.

    – Paulw11
    Nov 23 '18 at 5:57






  • 1





    Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.

    – Daniel Springer
    Nov 23 '18 at 6:08














1












1








1








On the click of a create button, I navigate to tableviewcontroller screen. But nothing is populated in the tableview since the array which populates the tableview hasn't been called yet.



Now once the tableview screen is reached, after a few seconds another method is called elsewhere(in another file), which in turn calls a function in this tableviewcontroller screen. This is that method of the tableviewcontroller screen which is called...



  func stopIndicator(thegrpName: String) {
stopIndicator()

let realm = try! Realm()
let chatGrp = realm.objects(ChatGroup.self)
chatGroup = chatGrp

tableview.reloadData() //CRASH HAPPENS HERE

}


In this method, once I reach tableview.reloadData() it crashes with the error Unexpectedly found nil while unwrapping an optional value..



I referred this link which seems to have a similar problem...but couldn't get much help out of it...



What could be the reason for this crash...?



EDIT 1: The numberOfRows and cellForRowAt.. is given like so...



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if let chatGrp = chatGroup {
return chatGrp.count
}
return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ChatGroupTableViewCell = tableView.dequeueReusableCell(withIdentifier: "chatgroupIdentifier") as! ChatGroupTableViewCell

let groupChatObj = chatGroup![indexPath.row]
cell.chatLabel.text = groupChatObj.lastMessage?.text?.text
return cell
}









share|improve this question
















On the click of a create button, I navigate to tableviewcontroller screen. But nothing is populated in the tableview since the array which populates the tableview hasn't been called yet.



Now once the tableview screen is reached, after a few seconds another method is called elsewhere(in another file), which in turn calls a function in this tableviewcontroller screen. This is that method of the tableviewcontroller screen which is called...



  func stopIndicator(thegrpName: String) {
stopIndicator()

let realm = try! Realm()
let chatGrp = realm.objects(ChatGroup.self)
chatGroup = chatGrp

tableview.reloadData() //CRASH HAPPENS HERE

}


In this method, once I reach tableview.reloadData() it crashes with the error Unexpectedly found nil while unwrapping an optional value..



I referred this link which seems to have a similar problem...but couldn't get much help out of it...



What could be the reason for this crash...?



EDIT 1: The numberOfRows and cellForRowAt.. is given like so...



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if let chatGrp = chatGroup {
return chatGrp.count
}
return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ChatGroupTableViewCell = tableView.dequeueReusableCell(withIdentifier: "chatgroupIdentifier") as! ChatGroupTableViewCell

let groupChatObj = chatGroup![indexPath.row]
cell.chatLabel.text = groupChatObj.lastMessage?.text?.text
return cell
}






ios swift uitableview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 5:44







user308123

















asked Nov 23 '18 at 5:30









user308123user308123

478




478













  • Please show your numberOfRowsInSection and cellForRowAt methods. Also see this for general troubleshooting and advice for this exception

    – Paulw11
    Nov 23 '18 at 5:39













  • Seems tableview is nil, check the outlet for connection if it's added in a storyboard.

    – Satish
    Nov 23 '18 at 5:43













  • checked that @Satish..its connected properly..

    – user308123
    Nov 23 '18 at 5:44






  • 2





    Set an exception breakpoint to see which line is crashing.

    – Paulw11
    Nov 23 '18 at 5:57






  • 1





    Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.

    – Daniel Springer
    Nov 23 '18 at 6:08



















  • Please show your numberOfRowsInSection and cellForRowAt methods. Also see this for general troubleshooting and advice for this exception

    – Paulw11
    Nov 23 '18 at 5:39













  • Seems tableview is nil, check the outlet for connection if it's added in a storyboard.

    – Satish
    Nov 23 '18 at 5:43













  • checked that @Satish..its connected properly..

    – user308123
    Nov 23 '18 at 5:44






  • 2





    Set an exception breakpoint to see which line is crashing.

    – Paulw11
    Nov 23 '18 at 5:57






  • 1





    Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.

    – Daniel Springer
    Nov 23 '18 at 6:08

















Please show your numberOfRowsInSection and cellForRowAt methods. Also see this for general troubleshooting and advice for this exception

– Paulw11
Nov 23 '18 at 5:39







Please show your numberOfRowsInSection and cellForRowAt methods. Also see this for general troubleshooting and advice for this exception

– Paulw11
Nov 23 '18 at 5:39















Seems tableview is nil, check the outlet for connection if it's added in a storyboard.

– Satish
Nov 23 '18 at 5:43







Seems tableview is nil, check the outlet for connection if it's added in a storyboard.

– Satish
Nov 23 '18 at 5:43















checked that @Satish..its connected properly..

– user308123
Nov 23 '18 at 5:44





checked that @Satish..its connected properly..

– user308123
Nov 23 '18 at 5:44




2




2





Set an exception breakpoint to see which line is crashing.

– Paulw11
Nov 23 '18 at 5:57





Set an exception breakpoint to see which line is crashing.

– Paulw11
Nov 23 '18 at 5:57




1




1





Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.

– Daniel Springer
Nov 23 '18 at 6:08





Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.

– Daniel Springer
Nov 23 '18 at 6:08












1 Answer
1






active

oldest

votes


















0














It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator you are calling method on singleton instead which gives you an error.



So, set delegate right. First create protocol



protocol YourProtocol {
func stopIndicator(thegrpName: String)
}


then in another file create delegate property



var delegate: YourProtocol?


now when you need to call delegate method stopIndicator call this



delegate?.stopIndicator(thegrpName: ...)


now add to your ViewController protocol



ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol


and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)



fileClass.delegate = self 





share|improve this answer


























  • ok..let me try @Robert Dresler..

    – user308123
    Nov 23 '18 at 6:28











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441080%2fcrash-at-tableview-reloaddata-with-error-unexpectedly-found-nil%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator you are calling method on singleton instead which gives you an error.



So, set delegate right. First create protocol



protocol YourProtocol {
func stopIndicator(thegrpName: String)
}


then in another file create delegate property



var delegate: YourProtocol?


now when you need to call delegate method stopIndicator call this



delegate?.stopIndicator(thegrpName: ...)


now add to your ViewController protocol



ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol


and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)



fileClass.delegate = self 





share|improve this answer


























  • ok..let me try @Robert Dresler..

    – user308123
    Nov 23 '18 at 6:28
















0














It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator you are calling method on singleton instead which gives you an error.



So, set delegate right. First create protocol



protocol YourProtocol {
func stopIndicator(thegrpName: String)
}


then in another file create delegate property



var delegate: YourProtocol?


now when you need to call delegate method stopIndicator call this



delegate?.stopIndicator(thegrpName: ...)


now add to your ViewController protocol



ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol


and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)



fileClass.delegate = self 





share|improve this answer


























  • ok..let me try @Robert Dresler..

    – user308123
    Nov 23 '18 at 6:28














0












0








0







It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator you are calling method on singleton instead which gives you an error.



So, set delegate right. First create protocol



protocol YourProtocol {
func stopIndicator(thegrpName: String)
}


then in another file create delegate property



var delegate: YourProtocol?


now when you need to call delegate method stopIndicator call this



delegate?.stopIndicator(thegrpName: ...)


now add to your ViewController protocol



ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol


and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)



fileClass.delegate = self 





share|improve this answer















It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator you are calling method on singleton instead which gives you an error.



So, set delegate right. First create protocol



protocol YourProtocol {
func stopIndicator(thegrpName: String)
}


then in another file create delegate property



var delegate: YourProtocol?


now when you need to call delegate method stopIndicator call this



delegate?.stopIndicator(thegrpName: ...)


now add to your ViewController protocol



ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol


and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)



fileClass.delegate = self 






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 7:10

























answered Nov 23 '18 at 6:26









Robert DreslerRobert Dresler

6,1152626




6,1152626













  • ok..let me try @Robert Dresler..

    – user308123
    Nov 23 '18 at 6:28



















  • ok..let me try @Robert Dresler..

    – user308123
    Nov 23 '18 at 6:28

















ok..let me try @Robert Dresler..

– user308123
Nov 23 '18 at 6:28





ok..let me try @Robert Dresler..

– user308123
Nov 23 '18 at 6:28




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441080%2fcrash-at-tableview-reloaddata-with-error-unexpectedly-found-nil%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen